1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
|
//===-- MsvcStlTree.cpp ---------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "MsvcStl.h"
#include "lldb/DataFormatters/FormattersHelpers.h"
#include "lldb/Utility/Status.h"
#include "lldb/ValueObject/ValueObject.h"
#include "lldb/lldb-enumerations.h"
#include "lldb/lldb-forward.h"
#include <cstdint>
#include <optional>
using namespace lldb;
using namespace lldb_private;
using namespace lldb_private::formatters;
// A Node looks as follows:
// struct _Tree_node {
// _Tree_node *_Left;
// _Tree_node *_Parent;
// _Tree_node *_Right;
// char _Color;
// char _Isnil; // true (!= 0) if head or nil node
// value_type _Myval;
// };
namespace {
class MapEntry {
public:
MapEntry() = default;
explicit MapEntry(ValueObjectSP entry_sp) : m_entry_sp(entry_sp) {}
explicit MapEntry(ValueObject *entry)
: m_entry_sp(entry ? entry->GetSP() : ValueObjectSP()) {}
ValueObjectSP left() const {
if (!m_entry_sp)
return m_entry_sp;
return m_entry_sp->GetSyntheticChildAtOffset(
0, m_entry_sp->GetCompilerType(), true);
}
ValueObjectSP right() const {
if (!m_entry_sp)
return m_entry_sp;
return m_entry_sp->GetSyntheticChildAtOffset(
2 * m_entry_sp->GetProcessSP()->GetAddressByteSize(),
m_entry_sp->GetCompilerType(), true);
}
ValueObjectSP parent() const {
if (!m_entry_sp)
return m_entry_sp;
return m_entry_sp->GetSyntheticChildAtOffset(
m_entry_sp->GetProcessSP()->GetAddressByteSize(),
m_entry_sp->GetCompilerType(), true);
}
uint64_t value() const {
if (!m_entry_sp)
return 0;
return m_entry_sp->GetValueAsUnsigned(0);
}
bool is_nil() const {
if (!m_entry_sp)
return true;
auto isnil_sp = m_entry_sp->GetChildMemberWithName("_Isnil");
if (!isnil_sp)
return true;
return isnil_sp->GetValueAsUnsigned(1) != 0;
}
bool error() const {
if (!m_entry_sp)
return true;
return m_entry_sp->GetError().Fail();
}
bool is_nullptr() const { return (value() == 0); }
ValueObjectSP GetEntry() const { return m_entry_sp; }
void SetEntry(ValueObjectSP entry) { m_entry_sp = entry; }
bool operator==(const MapEntry &rhs) const {
return (rhs.m_entry_sp.get() == m_entry_sp.get());
}
private:
ValueObjectSP m_entry_sp;
};
class MapIterator {
public:
MapIterator(ValueObject *entry, size_t depth = 0)
: m_entry(entry), m_max_depth(depth) {}
MapIterator() = default;
ValueObjectSP value() { return m_entry.GetEntry(); }
ValueObjectSP advance(size_t count) {
ValueObjectSP fail;
if (m_error)
return fail;
size_t steps = 0;
while (count > 0) {
next();
count--, steps++;
if (m_error || m_entry.is_nullptr() || (steps > m_max_depth))
return fail;
}
return m_entry.GetEntry();
}
private:
/// Mimicks _Tree_unchecked_const_iterator::operator++()
void next() {
if (m_entry.is_nullptr())
return;
MapEntry right(m_entry.right());
if (!right.is_nil()) {
m_entry = tree_min(std::move(right));
return;
}
size_t steps = 0;
MapEntry pnode(m_entry.parent());
while (!pnode.is_nil() &&
m_entry.value() == MapEntry(pnode.right()).value()) {
m_entry = pnode;
steps++;
if (steps > m_max_depth) {
m_entry = MapEntry();
return;
}
pnode.SetEntry(m_entry.parent());
}
m_entry = std::move(pnode);
}
/// Mimicks MSVC STL's _Min() algorithm (finding the leftmost node in the
/// subtree).
MapEntry tree_min(MapEntry pnode) {
if (pnode.is_nullptr())
return MapEntry();
MapEntry left(pnode.left());
size_t steps = 0;
while (!left.is_nil()) {
if (left.error()) {
m_error = true;
return MapEntry();
}
pnode = left;
left.SetEntry(pnode.left());
steps++;
if (steps > m_max_depth)
return MapEntry();
}
return pnode;
}
MapEntry m_entry;
size_t m_max_depth = 0;
bool m_error = false;
};
} // namespace
namespace lldb_private {
namespace formatters {
class MsvcStlTreeSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
public:
MsvcStlTreeSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp);
~MsvcStlTreeSyntheticFrontEnd() override = default;
llvm::Expected<uint32_t> CalculateNumChildren() override;
lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override;
lldb::ChildCacheState Update() override;
llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override;
private:
/// Returns the ValueObject for the _Tree_node at index \ref idx.
///
/// \param[in] idx The child index that we're looking to get the value for.
///
/// \param[in] max_depth The maximum search depth after which we stop trying
/// to find the node for.
///
/// \returns On success, returns the ValueObjectSP corresponding to the
/// _Tree_node's _Myval member.
/// On failure, nullptr is returned.
ValueObjectSP GetValueAt(size_t idx, size_t max_depth);
ValueObject *m_tree = nullptr;
ValueObject *m_begin_node = nullptr;
size_t m_count = UINT32_MAX;
std::map<size_t, MapIterator> m_iterators;
};
class MsvcStlTreeIterSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
public:
MsvcStlTreeIterSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
: SyntheticChildrenFrontEnd(*valobj_sp) {}
llvm::Expected<uint32_t> CalculateNumChildren() override {
if (!m_inner_sp)
return 0;
return m_inner_sp->GetNumChildren();
}
lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override {
if (!m_inner_sp)
return nullptr;
return m_inner_sp->GetChildAtIndex(idx);
}
lldb::ChildCacheState Update() override;
llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override {
if (!m_inner_sp)
return llvm::createStringError("There are no children.");
return m_inner_sp->GetIndexOfChildWithName(name);
}
lldb::ValueObjectSP GetSyntheticValue() override { return m_inner_sp; }
private:
ValueObjectSP m_inner_sp;
};
} // namespace formatters
} // namespace lldb_private
lldb_private::formatters::MsvcStlTreeSyntheticFrontEnd::
MsvcStlTreeSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
: SyntheticChildrenFrontEnd(*valobj_sp) {
if (valobj_sp)
Update();
}
llvm::Expected<uint32_t>
lldb_private::formatters::MsvcStlTreeSyntheticFrontEnd::CalculateNumChildren() {
if (m_count != UINT32_MAX)
return m_count;
if (m_tree == nullptr)
return 0;
if (auto node_sp = m_tree->GetChildMemberWithName("_Mysize")) {
m_count = node_sp->GetValueAsUnsigned(0);
return m_count;
}
return llvm::createStringError("Failed to read size.");
}
ValueObjectSP
lldb_private::formatters::MsvcStlTreeSyntheticFrontEnd::GetValueAt(
size_t idx, size_t max_depth) {
MapIterator iterator(m_begin_node, max_depth);
size_t advance_by = idx;
if (idx > 0) {
// If we have already created the iterator for the previous
// index, we can start from there and advance by 1.
auto cached_iterator = m_iterators.find(idx - 1);
if (cached_iterator != m_iterators.end()) {
iterator = cached_iterator->second;
advance_by = 1;
}
}
ValueObjectSP iterated_sp(iterator.advance(advance_by));
if (!iterated_sp)
// this tree is garbage - stop
return nullptr;
ValueObjectSP value_sp = iterated_sp->GetChildMemberWithName("_Myval");
if (!value_sp)
return nullptr;
m_iterators[idx] = iterator;
return value_sp;
}
lldb::ValueObjectSP
lldb_private::formatters::MsvcStlTreeSyntheticFrontEnd::GetChildAtIndex(
uint32_t idx) {
uint32_t num_children = CalculateNumChildrenIgnoringErrors();
if (idx >= num_children)
return nullptr;
if (m_tree == nullptr || m_begin_node == nullptr)
return nullptr;
ValueObjectSP val_sp = GetValueAt(idx, /*max_depth=*/num_children);
if (!val_sp) {
// this will stop all future searches until an Update() happens
m_tree = nullptr;
return nullptr;
}
// at this point we have a valid pair
// we need to copy current_sp into a new object otherwise we will end up with
// all items named _Myval
StreamString name;
name.Printf("[%" PRIu64 "]", (uint64_t)idx);
return val_sp->Clone(ConstString(name.GetString()));
}
lldb::ChildCacheState
lldb_private::formatters::MsvcStlTreeSyntheticFrontEnd::Update() {
m_count = UINT32_MAX;
m_tree = m_begin_node = nullptr;
m_iterators.clear();
m_tree =
m_backend.GetChildAtNamePath({"_Mypair", "_Myval2", "_Myval2"}).get();
if (!m_tree)
return lldb::ChildCacheState::eRefetch;
m_begin_node = m_tree->GetChildAtNamePath({"_Myhead", "_Left"}).get();
return lldb::ChildCacheState::eRefetch;
}
llvm::Expected<size_t>
lldb_private::formatters::MsvcStlTreeSyntheticFrontEnd::GetIndexOfChildWithName(
ConstString name) {
auto optional_idx = formatters::ExtractIndexFromString(name.GetCString());
if (!optional_idx) {
return llvm::createStringError("Type has no child named '%s'",
name.AsCString());
}
return *optional_idx;
}
lldb::ChildCacheState MsvcStlTreeIterSyntheticFrontEnd::Update() {
m_inner_sp = nullptr;
auto node_sp = m_backend.GetChildMemberWithName("_Ptr");
if (!node_sp)
return lldb::eRefetch;
MapEntry entry(node_sp.get());
if (entry.is_nil())
return lldb::eRefetch; // end
m_inner_sp = node_sp->GetChildMemberWithName("_Myval");
return lldb::eRefetch;
}
bool formatters::IsMsvcStlTreeIter(ValueObject &valobj) {
return valobj.GetChildMemberWithName("_Ptr") != nullptr;
}
bool formatters::MsvcStlTreeIterSummaryProvider(
ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
auto valobj_sp = valobj.GetNonSyntheticValue();
if (!valobj_sp)
return false;
auto node_sp = valobj_sp->GetChildMemberWithName("_Ptr");
if (!node_sp)
return false;
MapEntry entry(node_sp.get());
if (entry.is_nil()) {
stream.Printf("end");
return true;
}
auto value_sp = node_sp->GetChildMemberWithName("_Myval");
if (!value_sp)
return false;
auto *summary = value_sp->GetSummaryAsCString();
if (summary)
stream << summary;
return true;
}
SyntheticChildrenFrontEnd *
lldb_private::formatters::MsvcStlTreeIterSyntheticFrontEndCreator(
CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
return (valobj_sp ? new MsvcStlTreeIterSyntheticFrontEnd(valobj_sp)
: nullptr);
}
bool formatters::IsMsvcStlMapLike(ValueObject &valobj) {
return valobj.GetChildMemberWithName("_Mypair") != nullptr;
}
SyntheticChildrenFrontEnd *
lldb_private::formatters::MsvcStlMapLikeSyntheticFrontEndCreator(
lldb::ValueObjectSP valobj_sp) {
return (valobj_sp ? new MsvcStlTreeSyntheticFrontEnd(valobj_sp) : nullptr);
}
|