blob: d89869283cd363332b92a8f7458577e16ffd1f12 [file] [log] [blame]
Eugene Zelenko8d15f332015-10-20 01:10:59 +00001//===-- LibCxxList.cpp ------------------------------------------*- C++ -*-===//
Enrico Granata92373532013-03-19 22:58:48 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Eugene Zelenko8d15f332015-10-20 01:10:59 +000010// C Includes
11// C++ Includes
12// Other libraries and framework includes
13// Project includes
Enrico Granata33e97e62015-09-04 21:01:18 +000014#include "LibCxx.h"
Enrico Granata92373532013-03-19 22:58:48 +000015
16#include "lldb/Core/DataBufferHeap.h"
17#include "lldb/Core/Error.h"
18#include "lldb/Core/Stream.h"
19#include "lldb/Core/ValueObject.h"
20#include "lldb/Core/ValueObjectConstResult.h"
Enrico Granata419d7912015-09-04 00:33:51 +000021#include "lldb/DataFormatters/FormattersHelpers.h"
Enrico Granata92373532013-03-19 22:58:48 +000022#include "lldb/Host/Endian.h"
23#include "lldb/Symbol/ClangASTContext.h"
Enrico Granata92373532013-03-19 22:58:48 +000024#include "lldb/Target/Target.h"
25
26using namespace lldb;
27using namespace lldb_private;
28using namespace lldb_private::formatters;
29
30class MapEntry
31{
32public:
Eugene Zelenko8d15f332015-10-20 01:10:59 +000033 MapEntry() = default;
Enrico Granata395939a2014-12-16 21:28:16 +000034 explicit MapEntry (ValueObjectSP entry_sp) : m_entry_sp(entry_sp) {}
Eugene Zelenkobbd16812016-02-29 19:41:30 +000035 MapEntry(const MapEntry& rhs) = default;
Enrico Granata395939a2014-12-16 21:28:16 +000036 explicit MapEntry (ValueObject* entry) : m_entry_sp(entry ? entry->GetSP() : ValueObjectSP()) {}
Enrico Granata92373532013-03-19 22:58:48 +000037
38 ValueObjectSP
Enrico Granata395939a2014-12-16 21:28:16 +000039 left () const
Enrico Granata92373532013-03-19 22:58:48 +000040 {
Enrico Granata395939a2014-12-16 21:28:16 +000041 static ConstString g_left("__left_");
Enrico Granata92373532013-03-19 22:58:48 +000042 if (!m_entry_sp)
43 return m_entry_sp;
Enrico Granata395939a2014-12-16 21:28:16 +000044 return m_entry_sp->GetChildMemberWithName(g_left, true);
Enrico Granata92373532013-03-19 22:58:48 +000045 }
46
47 ValueObjectSP
Enrico Granata395939a2014-12-16 21:28:16 +000048 right () const
Enrico Granata92373532013-03-19 22:58:48 +000049 {
Enrico Granata395939a2014-12-16 21:28:16 +000050 static ConstString g_right("__right_");
Enrico Granata92373532013-03-19 22:58:48 +000051 if (!m_entry_sp)
52 return m_entry_sp;
Enrico Granata395939a2014-12-16 21:28:16 +000053 return m_entry_sp->GetChildMemberWithName(g_right, true);
Enrico Granata92373532013-03-19 22:58:48 +000054 }
55
56 ValueObjectSP
Enrico Granata395939a2014-12-16 21:28:16 +000057 parent () const
Enrico Granata92373532013-03-19 22:58:48 +000058 {
Enrico Granata395939a2014-12-16 21:28:16 +000059 static ConstString g_parent("__parent_");
Enrico Granata92373532013-03-19 22:58:48 +000060 if (!m_entry_sp)
61 return m_entry_sp;
Enrico Granata395939a2014-12-16 21:28:16 +000062 return m_entry_sp->GetChildMemberWithName(g_parent, true);
Enrico Granata92373532013-03-19 22:58:48 +000063 }
64
65 uint64_t
Enrico Granata395939a2014-12-16 21:28:16 +000066 value () const
Enrico Granata92373532013-03-19 22:58:48 +000067 {
68 if (!m_entry_sp)
69 return 0;
70 return m_entry_sp->GetValueAsUnsigned(0);
71 }
72
73 bool
Enrico Granata395939a2014-12-16 21:28:16 +000074 error () const
Enrico Granatad03a2732013-05-03 19:04:35 +000075 {
Enrico Granata4ffff272013-05-03 19:07:20 +000076 if (!m_entry_sp)
77 return true;
Enrico Granatad03a2732013-05-03 19:04:35 +000078 return m_entry_sp->GetError().Fail();
79 }
80
81 bool
Enrico Granata395939a2014-12-16 21:28:16 +000082 null() const
Enrico Granata92373532013-03-19 22:58:48 +000083 {
84 return (value() == 0);
85 }
86
87 ValueObjectSP
Enrico Granata395939a2014-12-16 21:28:16 +000088 GetEntry () const
Enrico Granata92373532013-03-19 22:58:48 +000089 {
90 return m_entry_sp;
91 }
92
93 void
94 SetEntry (ValueObjectSP entry)
95 {
96 m_entry_sp = entry;
97 }
98
99 bool
100 operator == (const MapEntry& rhs) const
101 {
102 return (rhs.m_entry_sp.get() == m_entry_sp.get());
103 }
104
105private:
106 ValueObjectSP m_entry_sp;
107};
108
109class MapIterator
110{
111public:
Eugene Zelenko8d15f332015-10-20 01:10:59 +0000112 MapIterator() = default;
Enrico Granatad03a2732013-05-03 19:04:35 +0000113 MapIterator (MapEntry entry, size_t depth = 0) : m_entry(entry), m_max_depth(depth), m_error(false) {}
114 MapIterator (ValueObjectSP entry, size_t depth = 0) : m_entry(entry), m_max_depth(depth), m_error(false) {}
115 MapIterator (const MapIterator& rhs) : m_entry(rhs.m_entry),m_max_depth(rhs.m_max_depth), m_error(false) {}
116 MapIterator (ValueObject* entry, size_t depth = 0) : m_entry(entry), m_max_depth(depth), m_error(false) {}
Enrico Granata92373532013-03-19 22:58:48 +0000117
118 ValueObjectSP
119 value ()
120 {
121 return m_entry.GetEntry();
122 }
123
124 ValueObjectSP
125 advance (size_t count)
126 {
Eugene Zelenkobbd16812016-02-29 19:41:30 +0000127 ValueObjectSP fail;
Enrico Granatad03a2732013-05-03 19:04:35 +0000128 if (m_error)
Enrico Granata395939a2014-12-16 21:28:16 +0000129 return fail;
Enrico Granata92373532013-03-19 22:58:48 +0000130 size_t steps = 0;
131 while (count > 0)
132 {
Enrico Granata395939a2014-12-16 21:28:16 +0000133 next();
134 count--, steps++;
135 if (m_error ||
136 m_entry.null() ||
137 (steps > m_max_depth))
138 return fail;
Enrico Granata92373532013-03-19 22:58:48 +0000139 }
140 return m_entry.GetEntry();
141 }
Enrico Granataa0b75d72015-12-04 22:25:52 +0000142
Enrico Granata92373532013-03-19 22:58:48 +0000143protected:
144 void
145 next ()
146 {
Enrico Granata395939a2014-12-16 21:28:16 +0000147 if (m_entry.null())
148 return;
149 MapEntry right(m_entry.right());
Eugene Zelenkobbd16812016-02-29 19:41:30 +0000150 if (!right.null())
Enrico Granata395939a2014-12-16 21:28:16 +0000151 {
152 m_entry = tree_min(std::move(right));
153 return;
154 }
155 size_t steps = 0;
156 while (!is_left_child(m_entry))
157 {
158 if (m_entry.error())
159 {
160 m_error = true;
161 return;
162 }
163 m_entry.SetEntry(m_entry.parent());
164 steps++;
165 if (steps > m_max_depth)
166 {
167 m_entry = MapEntry();
168 return;
169 }
170 }
171 m_entry = MapEntry(m_entry.parent());
Enrico Granata92373532013-03-19 22:58:48 +0000172 }
Enrico Granataa0b75d72015-12-04 22:25:52 +0000173
Enrico Granata92373532013-03-19 22:58:48 +0000174private:
Enrico Granata395939a2014-12-16 21:28:16 +0000175 MapEntry
176 tree_min (MapEntry&& x)
Enrico Granata92373532013-03-19 22:58:48 +0000177 {
Enrico Granata92373532013-03-19 22:58:48 +0000178 if (x.null())
Enrico Granata395939a2014-12-16 21:28:16 +0000179 return MapEntry();
Enrico Granata92373532013-03-19 22:58:48 +0000180 MapEntry left(x.left());
181 size_t steps = 0;
Eugene Zelenkobbd16812016-02-29 19:41:30 +0000182 while (!left.null())
Enrico Granata92373532013-03-19 22:58:48 +0000183 {
Enrico Granatad03a2732013-05-03 19:04:35 +0000184 if (left.error())
185 {
186 m_error = true;
Enrico Granata395939a2014-12-16 21:28:16 +0000187 return MapEntry();
Enrico Granatad03a2732013-05-03 19:04:35 +0000188 }
Enrico Granata395939a2014-12-16 21:28:16 +0000189 x = left;
Enrico Granata92373532013-03-19 22:58:48 +0000190 left.SetEntry(x.left());
191 steps++;
192 if (steps > m_max_depth)
Enrico Granata395939a2014-12-16 21:28:16 +0000193 return MapEntry();
Enrico Granata92373532013-03-19 22:58:48 +0000194 }
Enrico Granata395939a2014-12-16 21:28:16 +0000195 return x;
Enrico Granata92373532013-03-19 22:58:48 +0000196 }
Enrico Granataa0b75d72015-12-04 22:25:52 +0000197
Enrico Granata92373532013-03-19 22:58:48 +0000198 bool
Enrico Granata395939a2014-12-16 21:28:16 +0000199 is_left_child (const MapEntry& x)
Enrico Granata92373532013-03-19 22:58:48 +0000200 {
Enrico Granata92373532013-03-19 22:58:48 +0000201 if (x.null())
202 return false;
203 MapEntry rhs(x.parent());
204 rhs.SetEntry(rhs.left());
205 return x.value() == rhs.value();
206 }
207
Enrico Granata92373532013-03-19 22:58:48 +0000208 MapEntry m_entry;
209 size_t m_max_depth;
Enrico Granatad03a2732013-05-03 19:04:35 +0000210 bool m_error;
Enrico Granata92373532013-03-19 22:58:48 +0000211};
212
Enrico Granataa0b75d72015-12-04 22:25:52 +0000213namespace lldb_private {
214 namespace formatters {
215 class LibcxxStdMapSyntheticFrontEnd : public SyntheticChildrenFrontEnd
216 {
217 public:
218 LibcxxStdMapSyntheticFrontEnd (lldb::ValueObjectSP valobj_sp);
219
220 ~LibcxxStdMapSyntheticFrontEnd() override = default;
221
222 size_t
223 CalculateNumChildren() override;
224
225 lldb::ValueObjectSP
226 GetChildAtIndex(size_t idx) override;
227
228 bool
229 Update() override;
230
231 bool
232 MightHaveChildren() override;
233
234 size_t
235 GetIndexOfChildWithName(const ConstString &name) override;
236
237 private:
238 bool
239 GetDataType();
240
241 void
242 GetValueOffset (const lldb::ValueObjectSP& node);
243
244 ValueObject* m_tree;
245 ValueObject* m_root_node;
246 CompilerType m_element_type;
247 uint32_t m_skip_size;
248 size_t m_count;
Enrico Granataa0b75d72015-12-04 22:25:52 +0000249 std::map<size_t, MapIterator> m_iterators;
250 };
251 } // namespace formatters
252} // namespace lldb_private
253
Enrico Granata92373532013-03-19 22:58:48 +0000254lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::LibcxxStdMapSyntheticFrontEnd (lldb::ValueObjectSP valobj_sp) :
Eugene Zelenkobbd16812016-02-29 19:41:30 +0000255 SyntheticChildrenFrontEnd(*valobj_sp),
256 m_tree(nullptr),
257 m_root_node(nullptr),
258 m_element_type(),
259 m_skip_size(UINT32_MAX),
260 m_count(UINT32_MAX),
Eugene Zelenkobbd16812016-02-29 19:41:30 +0000261 m_iterators()
Enrico Granata92373532013-03-19 22:58:48 +0000262{
263 if (valobj_sp)
264 Update();
265}
266
267size_t
268lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::CalculateNumChildren ()
269{
Enrico Granata4c2bf562015-12-04 22:49:27 +0000270 static ConstString g___pair3_("__pair3_");
271 static ConstString g___first_("__first_");
272
Enrico Granata92373532013-03-19 22:58:48 +0000273 if (m_count != UINT32_MAX)
274 return m_count;
Eugene Zelenkobbd16812016-02-29 19:41:30 +0000275 if (m_tree == nullptr)
Enrico Granata92373532013-03-19 22:58:48 +0000276 return 0;
Enrico Granata4c2bf562015-12-04 22:49:27 +0000277 ValueObjectSP m_item(m_tree->GetChildMemberWithName(g___pair3_, true));
Enrico Granata92373532013-03-19 22:58:48 +0000278 if (!m_item)
279 return 0;
Enrico Granata4c2bf562015-12-04 22:49:27 +0000280 m_item = m_item->GetChildMemberWithName(g___first_, true);
Enrico Granata92373532013-03-19 22:58:48 +0000281 if (!m_item)
282 return 0;
283 m_count = m_item->GetValueAsUnsigned(0);
284 return m_count;
285}
286
287bool
288lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::GetDataType()
289{
Enrico Granata4c2bf562015-12-04 22:49:27 +0000290 static ConstString g___value_("__value_");
291
Greg Claytond8d4a572015-08-11 21:38:15 +0000292 if (m_element_type.GetOpaqueQualType() && m_element_type.GetTypeSystem())
Enrico Granata92373532013-03-19 22:58:48 +0000293 return true;
294 m_element_type.Clear();
295 ValueObjectSP deref;
296 Error error;
297 deref = m_root_node->Dereference(error);
298 if (!deref || error.Fail())
299 return false;
Enrico Granata4c2bf562015-12-04 22:49:27 +0000300 deref = deref->GetChildMemberWithName(g___value_, true);
Enrico Granata92373532013-03-19 22:58:48 +0000301 if (!deref)
302 return false;
Greg Clayton99558cc42015-08-24 23:46:31 +0000303 m_element_type = deref->GetCompilerType();
Enrico Granata92373532013-03-19 22:58:48 +0000304 return true;
305}
306
307void
308lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::GetValueOffset (const lldb::ValueObjectSP& node)
309{
310 if (m_skip_size != UINT32_MAX)
311 return;
312 if (!node)
313 return;
Greg Clayton99558cc42015-08-24 23:46:31 +0000314 CompilerType node_type(node->GetCompilerType());
Enrico Granata92373532013-03-19 22:58:48 +0000315 uint64_t bit_offset;
Eugene Zelenkobbd16812016-02-29 19:41:30 +0000316 if (node_type.GetIndexOfFieldWithName("__value_", nullptr, &bit_offset) == UINT32_MAX)
Enrico Granata92373532013-03-19 22:58:48 +0000317 return;
318 m_skip_size = bit_offset / 8u;
319}
320
321lldb::ValueObjectSP
322lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::GetChildAtIndex (size_t idx)
323{
Enrico Granata340fa532014-09-12 00:55:37 +0000324 static ConstString g___cc("__cc");
325 static ConstString g___nc("__nc");
Enrico Granata4c2bf562015-12-04 22:49:27 +0000326 static ConstString g___value_("__value_");
Enrico Granata340fa532014-09-12 00:55:37 +0000327
Enrico Granata92373532013-03-19 22:58:48 +0000328 if (idx >= CalculateNumChildren())
329 return lldb::ValueObjectSP();
Eugene Zelenkobbd16812016-02-29 19:41:30 +0000330 if (m_tree == nullptr || m_root_node == nullptr)
Enrico Granata92373532013-03-19 22:58:48 +0000331 return lldb::ValueObjectSP();
332
Enrico Granata92373532013-03-19 22:58:48 +0000333 MapIterator iterator(m_root_node, CalculateNumChildren());
Enrico Granataa0b75d72015-12-04 22:25:52 +0000334
335 const bool need_to_skip = (idx > 0);
336 size_t actual_advancde = idx;
337 if (need_to_skip)
338 {
339 auto cached_iterator = m_iterators.find(idx-1);
340 if (cached_iterator != m_iterators.end())
341 {
342 iterator = cached_iterator->second;
343 actual_advancde = 1;
344 }
345 }
346
347 ValueObjectSP iterated_sp(iterator.advance(actual_advancde));
Eugene Zelenkobbd16812016-02-29 19:41:30 +0000348 if (!iterated_sp)
Enrico Granata92373532013-03-19 22:58:48 +0000349 {
350 // this tree is garbage - stop
Eugene Zelenkobbd16812016-02-29 19:41:30 +0000351 m_tree = nullptr; // this will stop all future searches until an Update() happens
Enrico Granata92373532013-03-19 22:58:48 +0000352 return iterated_sp;
353 }
354 if (GetDataType())
355 {
356 if (!need_to_skip)
357 {
358 Error error;
359 iterated_sp = iterated_sp->Dereference(error);
360 if (!iterated_sp || error.Fail())
361 {
Eugene Zelenkobbd16812016-02-29 19:41:30 +0000362 m_tree = nullptr;
Enrico Granata92373532013-03-19 22:58:48 +0000363 return lldb::ValueObjectSP();
364 }
365 GetValueOffset(iterated_sp);
Enrico Granata4c2bf562015-12-04 22:49:27 +0000366 iterated_sp = iterated_sp->GetChildMemberWithName(g___value_, true);
Enrico Granata92373532013-03-19 22:58:48 +0000367 if (!iterated_sp)
368 {
Eugene Zelenkobbd16812016-02-29 19:41:30 +0000369 m_tree = nullptr;
Enrico Granata92373532013-03-19 22:58:48 +0000370 return lldb::ValueObjectSP();
371 }
372 }
373 else
374 {
375 // because of the way our debug info is made, we need to read item 0 first
376 // so that we can cache information used to generate other elements
377 if (m_skip_size == UINT32_MAX)
378 GetChildAtIndex(0);
379 if (m_skip_size == UINT32_MAX)
380 {
Eugene Zelenkobbd16812016-02-29 19:41:30 +0000381 m_tree = nullptr;
Enrico Granata92373532013-03-19 22:58:48 +0000382 return lldb::ValueObjectSP();
383 }
384 iterated_sp = iterated_sp->GetSyntheticChildAtOffset(m_skip_size, m_element_type, true);
385 if (!iterated_sp)
386 {
Eugene Zelenkobbd16812016-02-29 19:41:30 +0000387 m_tree = nullptr;
Enrico Granata92373532013-03-19 22:58:48 +0000388 return lldb::ValueObjectSP();
389 }
390 }
391 }
392 else
393 {
Eugene Zelenkobbd16812016-02-29 19:41:30 +0000394 m_tree = nullptr;
Enrico Granata92373532013-03-19 22:58:48 +0000395 return lldb::ValueObjectSP();
396 }
397 // at this point we have a valid
398 // we need to copy current_sp into a new object otherwise we will end up with all items named __value_
399 DataExtractor data;
Sean Callanan866e91c2014-02-28 22:27:53 +0000400 Error error;
401 iterated_sp->GetData(data, error);
402 if (error.Fail())
403 {
Eugene Zelenkobbd16812016-02-29 19:41:30 +0000404 m_tree = nullptr;
Sean Callanan866e91c2014-02-28 22:27:53 +0000405 return lldb::ValueObjectSP();
406 }
Enrico Granata92373532013-03-19 22:58:48 +0000407 StreamString name;
Deepak Panickal99fbc072014-03-03 15:39:47 +0000408 name.Printf("[%" PRIu64 "]", (uint64_t)idx);
Enrico Granatae29df232014-12-09 19:51:20 +0000409 auto potential_child_sp = CreateValueObjectFromData(name.GetData(), data, m_backend.GetExecutionContextRef(), m_element_type);
Enrico Granata340fa532014-09-12 00:55:37 +0000410 if (potential_child_sp)
411 {
412 switch (potential_child_sp->GetNumChildren())
413 {
414 case 1:
415 {
416 auto child0_sp = potential_child_sp->GetChildAtIndex(0, true);
417 if (child0_sp && child0_sp->GetName() == g___cc)
418 potential_child_sp = child0_sp;
419 break;
420 }
421 case 2:
422 {
423 auto child0_sp = potential_child_sp->GetChildAtIndex(0, true);
424 auto child1_sp = potential_child_sp->GetChildAtIndex(1, true);
425 if (child0_sp && child0_sp->GetName() == g___cc &&
426 child1_sp && child1_sp->GetName() == g___nc)
427 potential_child_sp = child0_sp;
428 break;
429 }
430 }
431 potential_child_sp->SetName(ConstString(name.GetData()));
432 }
Enrico Granataa0b75d72015-12-04 22:25:52 +0000433 m_iterators[idx] = iterator;
Enrico Granata106aae52016-07-06 21:24:28 +0000434 return potential_child_sp;
Enrico Granata92373532013-03-19 22:58:48 +0000435}
436
437bool
438lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::Update()
439{
Enrico Granata4c2bf562015-12-04 22:49:27 +0000440 static ConstString g___tree_("__tree_");
441 static ConstString g___begin_node_("__begin_node_");
Enrico Granata92373532013-03-19 22:58:48 +0000442 m_count = UINT32_MAX;
Eugene Zelenkobbd16812016-02-29 19:41:30 +0000443 m_tree = m_root_node = nullptr;
Enrico Granataa0b75d72015-12-04 22:25:52 +0000444 m_iterators.clear();
Enrico Granata4c2bf562015-12-04 22:49:27 +0000445 m_tree = m_backend.GetChildMemberWithName(g___tree_, true).get();
Enrico Granata92373532013-03-19 22:58:48 +0000446 if (!m_tree)
Matt Kopecef143712013-06-03 18:00:07 +0000447 return false;
Enrico Granata4c2bf562015-12-04 22:49:27 +0000448 m_root_node = m_tree->GetChildMemberWithName(g___begin_node_, true).get();
Enrico Granata92373532013-03-19 22:58:48 +0000449 return false;
450}
451
452bool
453lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::MightHaveChildren ()
454{
455 return true;
456}
457
458size_t
459lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::GetIndexOfChildWithName (const ConstString &name)
460{
461 return ExtractIndexFromString(name.GetCString());
462}
463
Enrico Granata92373532013-03-19 22:58:48 +0000464SyntheticChildrenFrontEnd*
465lldb_private::formatters::LibcxxStdMapSyntheticFrontEndCreator (CXXSyntheticChildren*, lldb::ValueObjectSP valobj_sp)
466{
Eugene Zelenkobbd16812016-02-29 19:41:30 +0000467 return (valobj_sp ? new LibcxxStdMapSyntheticFrontEnd(valobj_sp) : nullptr);
Enrico Granata92373532013-03-19 22:58:48 +0000468}