LLDB mainline
LibCxxList.cpp
Go to the documentation of this file.
1//===-- LibCxxList.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 "LibCxx.h"
10
15#include "lldb/Target/Target.h"
17#include "lldb/Utility/Endian.h"
18#include "lldb/Utility/Status.h"
19#include "lldb/Utility/Stream.h"
20
21using namespace lldb;
22using namespace lldb_private;
23using namespace lldb_private::formatters;
24
25namespace {
26
27class ListEntry {
28public:
29 ListEntry() = default;
30 ListEntry(ValueObjectSP entry_sp) : m_entry_sp(std::move(entry_sp)) {}
31 ListEntry(ValueObject *entry)
32 : m_entry_sp(entry ? entry->GetSP() : ValueObjectSP()) {}
33
34 ListEntry next() {
35 if (!m_entry_sp)
36 return ListEntry();
37 return ListEntry(m_entry_sp->GetChildMemberWithName("__next_", true));
38 }
39
40 ListEntry prev() {
41 if (!m_entry_sp)
42 return ListEntry();
43 return ListEntry(m_entry_sp->GetChildMemberWithName("__prev_", true));
44 }
45
46 uint64_t value() const {
47 if (!m_entry_sp)
48 return 0;
49 return m_entry_sp->GetValueAsUnsigned(0);
50 }
51
52 bool null() { return (value() == 0); }
53
54 explicit operator bool() { return GetEntry() && !null(); }
55
56 ValueObjectSP GetEntry() { return m_entry_sp; }
57
58 void SetEntry(ValueObjectSP entry) { m_entry_sp = entry; }
59
60 bool operator==(const ListEntry &rhs) const { return value() == rhs.value(); }
61
62 bool operator!=(const ListEntry &rhs) const { return !(*this == rhs); }
63
64private:
65 ValueObjectSP m_entry_sp;
66};
67
68class ListIterator {
69public:
70 ListIterator() = default;
71 ListIterator(ListEntry entry) : m_entry(std::move(entry)) {}
72 ListIterator(ValueObjectSP entry) : m_entry(std::move(entry)) {}
73 ListIterator(ValueObject *entry) : m_entry(entry) {}
74
75 ValueObjectSP value() { return m_entry.GetEntry(); }
76
77 ValueObjectSP advance(size_t count) {
78 if (count == 0)
79 return m_entry.GetEntry();
80 if (count == 1) {
81 next();
82 return m_entry.GetEntry();
83 }
84 while (count > 0) {
85 next();
86 count--;
87 if (m_entry.null())
88 return lldb::ValueObjectSP();
89 }
90 return m_entry.GetEntry();
91 }
92
93 bool operator==(const ListIterator &rhs) const {
94 return (rhs.m_entry == m_entry);
95 }
96
97protected:
98 void next() { m_entry = m_entry.next(); }
99
100 void prev() { m_entry = m_entry.prev(); }
101
102private:
103 ListEntry m_entry;
104};
105
106class AbstractListFrontEnd : public SyntheticChildrenFrontEnd {
107public:
108 size_t GetIndexOfChildWithName(ConstString name) override {
109 return ExtractIndexFromString(name.GetCString());
110 }
111 bool MightHaveChildren() override { return true; }
112 bool Update() override;
113
114protected:
115 AbstractListFrontEnd(ValueObject &valobj)
116 : SyntheticChildrenFrontEnd(valobj) {}
117
118 size_t m_count = 0;
119 ValueObject *m_head = nullptr;
120
121 static constexpr bool g_use_loop_detect = true;
122 size_t m_loop_detected = 0; // The number of elements that have had loop
123 // detection run over them.
124 ListEntry m_slow_runner; // Used for loop detection
125 ListEntry m_fast_runner; // Used for loop detection
126
127 size_t m_list_capping_size = 0;
128 CompilerType m_element_type;
129 std::map<size_t, ListIterator> m_iterators;
130
131 bool HasLoop(size_t count);
132 ValueObjectSP GetItem(size_t idx);
133};
134
135class ForwardListFrontEnd : public AbstractListFrontEnd {
136public:
137 ForwardListFrontEnd(ValueObject &valobj);
138
139 size_t CalculateNumChildren() override;
140 ValueObjectSP GetChildAtIndex(size_t idx) override;
141 bool Update() override;
142};
143
144class ListFrontEnd : public AbstractListFrontEnd {
145public:
146 ListFrontEnd(lldb::ValueObjectSP valobj_sp);
147
148 ~ListFrontEnd() override = default;
149
150 size_t CalculateNumChildren() override;
151
152 lldb::ValueObjectSP GetChildAtIndex(size_t idx) override;
153
154 bool Update() override;
155
156private:
157 lldb::addr_t m_node_address = 0;
158 ValueObject *m_tail = nullptr;
159};
160
161} // end anonymous namespace
162
163bool AbstractListFrontEnd::Update() {
164 m_loop_detected = 0;
165 m_count = UINT32_MAX;
166 m_head = nullptr;
167 m_list_capping_size = 0;
168 m_slow_runner.SetEntry(nullptr);
169 m_fast_runner.SetEntry(nullptr);
170 m_iterators.clear();
171
172 if (m_backend.GetTargetSP())
173 m_list_capping_size =
174 m_backend.GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
175 if (m_list_capping_size == 0)
176 m_list_capping_size = 255;
177
178 CompilerType list_type = m_backend.GetCompilerType();
179 if (list_type.IsReferenceType())
180 list_type = list_type.GetNonReferenceType();
181
182 if (list_type.GetNumTemplateArguments() == 0)
183 return false;
184 m_element_type = list_type.GetTypeTemplateArgument(0);
185
186 return false;
187}
188
189bool AbstractListFrontEnd::HasLoop(size_t count) {
190 if (!g_use_loop_detect)
191 return false;
192 // don't bother checking for a loop if we won't actually need to jump nodes
193 if (m_count < 2)
194 return false;
195
196 if (m_loop_detected == 0) {
197 // This is the first time we are being run (after the last update). Set up
198 // the loop invariant for the first element.
199 m_slow_runner = ListEntry(m_head).next();
200 m_fast_runner = m_slow_runner.next();
201 m_loop_detected = 1;
202 }
203
204 // Loop invariant:
205 // Loop detection has been run over the first m_loop_detected elements. If
206 // m_slow_runner == m_fast_runner then the loop has been detected after
207 // m_loop_detected elements.
208 const size_t steps_to_run = std::min(count, m_count);
209 while (m_loop_detected < steps_to_run && m_slow_runner && m_fast_runner &&
210 m_slow_runner != m_fast_runner) {
211
212 m_slow_runner = m_slow_runner.next();
213 m_fast_runner = m_fast_runner.next().next();
214 m_loop_detected++;
215 }
216 if (count <= m_loop_detected)
217 return false; // No loop in the first m_loop_detected elements.
218 if (!m_slow_runner || !m_fast_runner)
219 return false; // Reached the end of the list. Definitely no loops.
220 return m_slow_runner == m_fast_runner;
221}
222
223ValueObjectSP AbstractListFrontEnd::GetItem(size_t idx) {
224 size_t advance = idx;
225 ListIterator current(m_head);
226 if (idx > 0) {
227 auto cached_iterator = m_iterators.find(idx - 1);
228 if (cached_iterator != m_iterators.end()) {
229 current = cached_iterator->second;
230 advance = 1;
231 }
232 }
233 ValueObjectSP value_sp = current.advance(advance);
234 m_iterators[idx] = current;
235 return value_sp;
236}
237
238ForwardListFrontEnd::ForwardListFrontEnd(ValueObject &valobj)
239 : AbstractListFrontEnd(valobj) {
240 Update();
241}
242
243size_t ForwardListFrontEnd::CalculateNumChildren() {
244 if (m_count != UINT32_MAX)
245 return m_count;
246
247 ListEntry current(m_head);
248 m_count = 0;
249 while (current && m_count < m_list_capping_size) {
250 ++m_count;
251 current = current.next();
252 }
253 return m_count;
254}
255
256ValueObjectSP ForwardListFrontEnd::GetChildAtIndex(size_t idx) {
257 if (idx >= CalculateNumChildren())
258 return nullptr;
259
260 if (!m_head)
261 return nullptr;
262
263 if (HasLoop(idx + 1))
264 return nullptr;
265
266 ValueObjectSP current_sp = GetItem(idx);
267 if (!current_sp)
268 return nullptr;
269
270 current_sp = current_sp->GetChildAtIndex(1, true); // get the __value_ child
271 if (!current_sp)
272 return nullptr;
273
274 // we need to copy current_sp into a new object otherwise we will end up with
275 // all items named __value_
276 DataExtractor data;
278 current_sp->GetData(data, error);
279 if (error.Fail())
280 return nullptr;
281
282 return CreateValueObjectFromData(llvm::formatv("[{0}]", idx).str(), data,
283 m_backend.GetExecutionContextRef(),
284 m_element_type);
285}
286
287bool ForwardListFrontEnd::Update() {
288 AbstractListFrontEnd::Update();
289
290 Status err;
291 ValueObjectSP backend_addr(m_backend.AddressOf(err));
292 if (err.Fail() || !backend_addr)
293 return false;
294
295 ValueObjectSP impl_sp(
296 m_backend.GetChildMemberWithName("__before_begin_", true));
297 if (!impl_sp)
298 return false;
299 impl_sp = GetFirstValueOfLibCXXCompressedPair(*impl_sp);
300 if (!impl_sp)
301 return false;
302 m_head = impl_sp->GetChildMemberWithName("__next_", true).get();
303 return false;
304}
305
306ListFrontEnd::ListFrontEnd(lldb::ValueObjectSP valobj_sp)
307 : AbstractListFrontEnd(*valobj_sp) {
308 if (valobj_sp)
309 Update();
310}
311
312size_t ListFrontEnd::CalculateNumChildren() {
313 if (m_count != UINT32_MAX)
314 return m_count;
315 if (!m_head || !m_tail || m_node_address == 0)
316 return 0;
317 ValueObjectSP size_alloc(
318 m_backend.GetChildMemberWithName("__size_alloc_", true));
319 if (size_alloc) {
320 ValueObjectSP value = GetFirstValueOfLibCXXCompressedPair(*size_alloc);
321 if (value) {
322 m_count = value->GetValueAsUnsigned(UINT32_MAX);
323 }
324 }
325 if (m_count != UINT32_MAX) {
326 return m_count;
327 } else {
328 uint64_t next_val = m_head->GetValueAsUnsigned(0);
329 uint64_t prev_val = m_tail->GetValueAsUnsigned(0);
330 if (next_val == 0 || prev_val == 0)
331 return 0;
332 if (next_val == m_node_address)
333 return 0;
334 if (next_val == prev_val)
335 return 1;
336 uint64_t size = 2;
337 ListEntry current(m_head);
338 while (current.next() && current.next().value() != m_node_address) {
339 size++;
340 current = current.next();
341 if (size > m_list_capping_size)
342 break;
343 }
344 return m_count = (size - 1);
345 }
346}
347
348lldb::ValueObjectSP ListFrontEnd::GetChildAtIndex(size_t idx) {
349 static ConstString g_value("__value_");
350 static ConstString g_next("__next_");
351
352 if (idx >= CalculateNumChildren())
353 return lldb::ValueObjectSP();
354
355 if (!m_head || !m_tail || m_node_address == 0)
356 return lldb::ValueObjectSP();
357
358 if (HasLoop(idx + 1))
359 return lldb::ValueObjectSP();
360
361 ValueObjectSP current_sp = GetItem(idx);
362 if (!current_sp)
363 return lldb::ValueObjectSP();
364
365 current_sp = current_sp->GetChildAtIndex(1, true); // get the __value_ child
366 if (!current_sp)
367 return lldb::ValueObjectSP();
368
369 if (current_sp->GetName() == g_next) {
370 ProcessSP process_sp(current_sp->GetProcessSP());
371 if (!process_sp)
372 return lldb::ValueObjectSP();
373
374 // if we grabbed the __next_ pointer, then the child is one pointer deep-er
375 lldb::addr_t addr = current_sp->GetParent()->GetPointerValue();
376 addr = addr + 2 * process_sp->GetAddressByteSize();
377 ExecutionContext exe_ctx(process_sp);
378 current_sp =
379 CreateValueObjectFromAddress("__value_", addr, exe_ctx, m_element_type);
380 if (!current_sp)
381 return lldb::ValueObjectSP();
382 }
383
384 // we need to copy current_sp into a new object otherwise we will end up with
385 // all items named __value_
386 DataExtractor data;
388 current_sp->GetData(data, error);
389 if (error.Fail())
390 return lldb::ValueObjectSP();
391
392 StreamString name;
393 name.Printf("[%" PRIu64 "]", (uint64_t)idx);
394 return CreateValueObjectFromData(name.GetString(), data,
395 m_backend.GetExecutionContextRef(),
396 m_element_type);
397}
398
399bool ListFrontEnd::Update() {
400 AbstractListFrontEnd::Update();
401 m_tail = nullptr;
402 m_node_address = 0;
403
404 Status err;
405 ValueObjectSP backend_addr(m_backend.AddressOf(err));
406 if (err.Fail() || !backend_addr)
407 return false;
408 m_node_address = backend_addr->GetValueAsUnsigned(0);
409 if (!m_node_address || m_node_address == LLDB_INVALID_ADDRESS)
410 return false;
411 ValueObjectSP impl_sp(m_backend.GetChildMemberWithName("__end_", true));
412 if (!impl_sp)
413 return false;
414 m_head = impl_sp->GetChildMemberWithName("__next_", true).get();
415 m_tail = impl_sp->GetChildMemberWithName("__prev_", true).get();
416 return false;
417}
418
420 CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
421 return (valobj_sp ? new ListFrontEnd(valobj_sp) : nullptr);
422}
423
426 CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
427 return valobj_sp ? new ForwardListFrontEnd(*valobj_sp) : nullptr;
428}
static llvm::raw_ostream & error(Stream &strm)
static size_t CalculateNumChildren(CompilerType container_type, CompilerType element_type, lldb_private::ExecutionContextScope *exe_scope=nullptr)
Definition: VectorType.cpp:172
Generic representation of a type in a programming language.
Definition: CompilerType.h:36
CompilerType GetTypeTemplateArgument(size_t idx, bool expand_pack=false) const
size_t GetNumTemplateArguments(bool expand_pack=false) const
Return the number of template arguments the type has.
CompilerType GetNonReferenceType() const
If this type is a reference to a type (L value or R value reference), return a new type with the refe...
bool IsReferenceType(CompilerType *pointee_type=nullptr, bool *is_rvalue=nullptr) const
A uniqued constant string class.
Definition: ConstString.h:40
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:221
An data extractor class.
Definition: DataExtractor.h:48
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
An error handling class.
Definition: Status.h:44
bool Fail() const
Test for error condition.
Definition: Status.cpp:181
llvm::StringRef GetString() const
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
virtual size_t GetIndexOfChildWithName(ConstString name)=0
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:74
#define UINT32_MAX
Definition: lldb-defines.h:19
size_t ExtractIndexFromString(const char *item_name)
lldb::ValueObjectSP GetFirstValueOfLibCXXCompressedPair(ValueObject &pair)
Definition: LibCxx.cpp:50
SyntheticChildrenFrontEnd * LibcxxStdListSyntheticFrontEndCreator(CXXSyntheticChildren *, lldb::ValueObjectSP)
Definition: LibCxxList.cpp:419
SyntheticChildrenFrontEnd * LibcxxStdForwardListSyntheticFrontEndCreator(CXXSyntheticChildren *, lldb::ValueObjectSP)
Definition: LibCxxList.cpp:425
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
bool operator!=(const Address &lhs, const Address &rhs)
Definition: Address.cpp:1022
Definition: SBAddress.h:15
uint64_t addr_t
Definition: lldb-types.h:79
bool LLDB_API operator==(const SBAddress &lhs, const SBAddress &rhs)
Definition: SBAddress.cpp:60