blob: 37e0f1bcfe25cb5add8f5143906e89ab3f0410ae [file] [log] [blame]
Enrico Granata92373532013-03-19 22:58:48 +00001//===-- LibCxxList.cpp -------------------------------------------*- C++ -*-===//
2//
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
10#include "lldb/DataFormatters/CXXFormatterFunctions.h"
11
12#include "lldb/Core/DataBufferHeap.h"
13#include "lldb/Core/Error.h"
14#include "lldb/Core/Stream.h"
15#include "lldb/Core/ValueObject.h"
16#include "lldb/Core/ValueObjectConstResult.h"
17#include "lldb/Host/Endian.h"
18#include "lldb/Symbol/ClangASTContext.h"
19#include "lldb/Target/ObjCLanguageRuntime.h"
20#include "lldb/Target/Target.h"
21
22using namespace lldb;
23using namespace lldb_private;
24using namespace lldb_private::formatters;
25
Enrico Granatae85fe3a2014-10-22 20:34:38 +000026namespace lldb_private {
27 namespace formatters {
28 class LibcxxStdMapSyntheticFrontEnd : public SyntheticChildrenFrontEnd
29 {
30 public:
31 LibcxxStdMapSyntheticFrontEnd (lldb::ValueObjectSP valobj_sp);
32
33 virtual size_t
34 CalculateNumChildren ();
35
36 virtual lldb::ValueObjectSP
37 GetChildAtIndex (size_t idx);
38
39 virtual bool
40 Update();
41
42 virtual bool
43 MightHaveChildren ();
44
45 virtual size_t
46 GetIndexOfChildWithName (const ConstString &name);
47
48 virtual
49 ~LibcxxStdMapSyntheticFrontEnd ();
50 private:
51 bool
52 GetDataType();
53
54 void
55 GetValueOffset (const lldb::ValueObjectSP& node);
56
57 ValueObject* m_tree;
58 ValueObject* m_root_node;
Greg Claytona1e5dc82015-08-11 22:53:00 +000059 CompilerType m_element_type;
Enrico Granatae85fe3a2014-10-22 20:34:38 +000060 uint32_t m_skip_size;
61 size_t m_count;
62 std::map<size_t,lldb::ValueObjectSP> m_children;
63 };
64 }
65}
66
Enrico Granata92373532013-03-19 22:58:48 +000067class MapEntry
68{
69public:
70 MapEntry () {}
Enrico Granata395939a2014-12-16 21:28:16 +000071 explicit MapEntry (ValueObjectSP entry_sp) : m_entry_sp(entry_sp) {}
Enrico Granata92373532013-03-19 22:58:48 +000072 MapEntry (const MapEntry& rhs) : m_entry_sp(rhs.m_entry_sp) {}
Enrico Granata395939a2014-12-16 21:28:16 +000073 explicit MapEntry (ValueObject* entry) : m_entry_sp(entry ? entry->GetSP() : ValueObjectSP()) {}
Enrico Granata92373532013-03-19 22:58:48 +000074
75 ValueObjectSP
Enrico Granata395939a2014-12-16 21:28:16 +000076 left () const
Enrico Granata92373532013-03-19 22:58:48 +000077 {
Enrico Granata395939a2014-12-16 21:28:16 +000078 static ConstString g_left("__left_");
Enrico Granata92373532013-03-19 22:58:48 +000079 if (!m_entry_sp)
80 return m_entry_sp;
Enrico Granata395939a2014-12-16 21:28:16 +000081 return m_entry_sp->GetChildMemberWithName(g_left, true);
Enrico Granata92373532013-03-19 22:58:48 +000082 }
83
84 ValueObjectSP
Enrico Granata395939a2014-12-16 21:28:16 +000085 right () const
Enrico Granata92373532013-03-19 22:58:48 +000086 {
Enrico Granata395939a2014-12-16 21:28:16 +000087 static ConstString g_right("__right_");
Enrico Granata92373532013-03-19 22:58:48 +000088 if (!m_entry_sp)
89 return m_entry_sp;
Enrico Granata395939a2014-12-16 21:28:16 +000090 return m_entry_sp->GetChildMemberWithName(g_right, true);
Enrico Granata92373532013-03-19 22:58:48 +000091 }
92
93 ValueObjectSP
Enrico Granata395939a2014-12-16 21:28:16 +000094 parent () const
Enrico Granata92373532013-03-19 22:58:48 +000095 {
Enrico Granata395939a2014-12-16 21:28:16 +000096 static ConstString g_parent("__parent_");
Enrico Granata92373532013-03-19 22:58:48 +000097 if (!m_entry_sp)
98 return m_entry_sp;
Enrico Granata395939a2014-12-16 21:28:16 +000099 return m_entry_sp->GetChildMemberWithName(g_parent, true);
Enrico Granata92373532013-03-19 22:58:48 +0000100 }
101
102 uint64_t
Enrico Granata395939a2014-12-16 21:28:16 +0000103 value () const
Enrico Granata92373532013-03-19 22:58:48 +0000104 {
105 if (!m_entry_sp)
106 return 0;
107 return m_entry_sp->GetValueAsUnsigned(0);
108 }
109
110 bool
Enrico Granata395939a2014-12-16 21:28:16 +0000111 error () const
Enrico Granatad03a2732013-05-03 19:04:35 +0000112 {
Enrico Granata4ffff272013-05-03 19:07:20 +0000113 if (!m_entry_sp)
114 return true;
Enrico Granatad03a2732013-05-03 19:04:35 +0000115 return m_entry_sp->GetError().Fail();
116 }
117
118 bool
Enrico Granata395939a2014-12-16 21:28:16 +0000119 null() const
Enrico Granata92373532013-03-19 22:58:48 +0000120 {
121 return (value() == 0);
122 }
123
124 ValueObjectSP
Enrico Granata395939a2014-12-16 21:28:16 +0000125 GetEntry () const
Enrico Granata92373532013-03-19 22:58:48 +0000126 {
127 return m_entry_sp;
128 }
129
130 void
131 SetEntry (ValueObjectSP entry)
132 {
133 m_entry_sp = entry;
134 }
135
136 bool
137 operator == (const MapEntry& rhs) const
138 {
139 return (rhs.m_entry_sp.get() == m_entry_sp.get());
140 }
141
142private:
143 ValueObjectSP m_entry_sp;
144};
145
146class MapIterator
147{
148public:
149 MapIterator () {}
Enrico Granatad03a2732013-05-03 19:04:35 +0000150 MapIterator (MapEntry entry, size_t depth = 0) : m_entry(entry), m_max_depth(depth), m_error(false) {}
151 MapIterator (ValueObjectSP entry, size_t depth = 0) : m_entry(entry), m_max_depth(depth), m_error(false) {}
152 MapIterator (const MapIterator& rhs) : m_entry(rhs.m_entry),m_max_depth(rhs.m_max_depth), m_error(false) {}
153 MapIterator (ValueObject* entry, size_t depth = 0) : m_entry(entry), m_max_depth(depth), m_error(false) {}
Enrico Granata92373532013-03-19 22:58:48 +0000154
155 ValueObjectSP
156 value ()
157 {
158 return m_entry.GetEntry();
159 }
160
161 ValueObjectSP
162 advance (size_t count)
163 {
Enrico Granata395939a2014-12-16 21:28:16 +0000164 ValueObjectSP fail(nullptr);
Enrico Granatad03a2732013-05-03 19:04:35 +0000165 if (m_error)
Enrico Granata395939a2014-12-16 21:28:16 +0000166 return fail;
Enrico Granata92373532013-03-19 22:58:48 +0000167 size_t steps = 0;
168 while (count > 0)
169 {
Enrico Granata395939a2014-12-16 21:28:16 +0000170 next();
171 count--, steps++;
172 if (m_error ||
173 m_entry.null() ||
174 (steps > m_max_depth))
175 return fail;
Enrico Granata92373532013-03-19 22:58:48 +0000176 }
177 return m_entry.GetEntry();
178 }
179protected:
180 void
181 next ()
182 {
Enrico Granata395939a2014-12-16 21:28:16 +0000183 if (m_entry.null())
184 return;
185 MapEntry right(m_entry.right());
186 if (right.null() == false)
187 {
188 m_entry = tree_min(std::move(right));
189 return;
190 }
191 size_t steps = 0;
192 while (!is_left_child(m_entry))
193 {
194 if (m_entry.error())
195 {
196 m_error = true;
197 return;
198 }
199 m_entry.SetEntry(m_entry.parent());
200 steps++;
201 if (steps > m_max_depth)
202 {
203 m_entry = MapEntry();
204 return;
205 }
206 }
207 m_entry = MapEntry(m_entry.parent());
Enrico Granata92373532013-03-19 22:58:48 +0000208 }
209
210private:
Enrico Granata395939a2014-12-16 21:28:16 +0000211 MapEntry
212 tree_min (MapEntry&& x)
Enrico Granata92373532013-03-19 22:58:48 +0000213 {
Enrico Granata92373532013-03-19 22:58:48 +0000214 if (x.null())
Enrico Granata395939a2014-12-16 21:28:16 +0000215 return MapEntry();
Enrico Granata92373532013-03-19 22:58:48 +0000216 MapEntry left(x.left());
217 size_t steps = 0;
218 while (left.null() == false)
219 {
Enrico Granatad03a2732013-05-03 19:04:35 +0000220 if (left.error())
221 {
222 m_error = true;
Enrico Granata395939a2014-12-16 21:28:16 +0000223 return MapEntry();
Enrico Granatad03a2732013-05-03 19:04:35 +0000224 }
Enrico Granata395939a2014-12-16 21:28:16 +0000225 x = left;
Enrico Granata92373532013-03-19 22:58:48 +0000226 left.SetEntry(x.left());
227 steps++;
228 if (steps > m_max_depth)
Enrico Granata395939a2014-12-16 21:28:16 +0000229 return MapEntry();
Enrico Granata92373532013-03-19 22:58:48 +0000230 }
Enrico Granata395939a2014-12-16 21:28:16 +0000231 return x;
Enrico Granata92373532013-03-19 22:58:48 +0000232 }
Enrico Granata395939a2014-12-16 21:28:16 +0000233
Enrico Granata92373532013-03-19 22:58:48 +0000234 bool
Enrico Granata395939a2014-12-16 21:28:16 +0000235 is_left_child (const MapEntry& x)
Enrico Granata92373532013-03-19 22:58:48 +0000236 {
Enrico Granata92373532013-03-19 22:58:48 +0000237 if (x.null())
238 return false;
239 MapEntry rhs(x.parent());
240 rhs.SetEntry(rhs.left());
241 return x.value() == rhs.value();
242 }
243
Enrico Granata92373532013-03-19 22:58:48 +0000244 MapEntry m_entry;
245 size_t m_max_depth;
Enrico Granatad03a2732013-05-03 19:04:35 +0000246 bool m_error;
Enrico Granata92373532013-03-19 22:58:48 +0000247};
248
249lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::LibcxxStdMapSyntheticFrontEnd (lldb::ValueObjectSP valobj_sp) :
250SyntheticChildrenFrontEnd(*valobj_sp.get()),
251m_tree(NULL),
252m_root_node(NULL),
253m_element_type(),
Enrico Granata92373532013-03-19 22:58:48 +0000254m_skip_size(UINT32_MAX),
255m_count(UINT32_MAX),
256m_children()
257{
258 if (valobj_sp)
259 Update();
260}
261
262size_t
263lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::CalculateNumChildren ()
264{
265 if (m_count != UINT32_MAX)
266 return m_count;
267 if (m_tree == NULL)
268 return 0;
269 ValueObjectSP m_item(m_tree->GetChildMemberWithName(ConstString("__pair3_"), true));
270 if (!m_item)
271 return 0;
272 m_item = m_item->GetChildMemberWithName(ConstString("__first_"), true);
273 if (!m_item)
274 return 0;
275 m_count = m_item->GetValueAsUnsigned(0);
276 return m_count;
277}
278
279bool
280lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::GetDataType()
281{
Greg Claytond8d4a572015-08-11 21:38:15 +0000282 if (m_element_type.GetOpaqueQualType() && m_element_type.GetTypeSystem())
Enrico Granata92373532013-03-19 22:58:48 +0000283 return true;
284 m_element_type.Clear();
285 ValueObjectSP deref;
286 Error error;
287 deref = m_root_node->Dereference(error);
288 if (!deref || error.Fail())
289 return false;
290 deref = deref->GetChildMemberWithName(ConstString("__value_"), true);
291 if (!deref)
292 return false;
Greg Clayton99558cc42015-08-24 23:46:31 +0000293 m_element_type = deref->GetCompilerType();
Enrico Granata92373532013-03-19 22:58:48 +0000294 return true;
295}
296
297void
298lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::GetValueOffset (const lldb::ValueObjectSP& node)
299{
300 if (m_skip_size != UINT32_MAX)
301 return;
302 if (!node)
303 return;
Greg Clayton99558cc42015-08-24 23:46:31 +0000304 CompilerType node_type(node->GetCompilerType());
Enrico Granata92373532013-03-19 22:58:48 +0000305 uint64_t bit_offset;
Greg Clayton57ee3062013-07-11 22:46:58 +0000306 if (node_type.GetIndexOfFieldWithName("__value_", NULL, &bit_offset) == UINT32_MAX)
Enrico Granata92373532013-03-19 22:58:48 +0000307 return;
308 m_skip_size = bit_offset / 8u;
309}
310
311lldb::ValueObjectSP
312lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::GetChildAtIndex (size_t idx)
313{
Enrico Granata340fa532014-09-12 00:55:37 +0000314 static ConstString g___cc("__cc");
315 static ConstString g___nc("__nc");
316
317
Enrico Granata92373532013-03-19 22:58:48 +0000318 if (idx >= CalculateNumChildren())
319 return lldb::ValueObjectSP();
320 if (m_tree == NULL || m_root_node == NULL)
321 return lldb::ValueObjectSP();
322
323 auto cached = m_children.find(idx);
324 if (cached != m_children.end())
325 return cached->second;
326
327 bool need_to_skip = (idx > 0);
328 MapIterator iterator(m_root_node, CalculateNumChildren());
329 ValueObjectSP iterated_sp(iterator.advance(idx));
330 if (iterated_sp.get() == NULL)
331 {
332 // this tree is garbage - stop
333 m_tree = NULL; // this will stop all future searches until an Update() happens
334 return iterated_sp;
335 }
336 if (GetDataType())
337 {
338 if (!need_to_skip)
339 {
340 Error error;
341 iterated_sp = iterated_sp->Dereference(error);
342 if (!iterated_sp || error.Fail())
343 {
344 m_tree = NULL;
345 return lldb::ValueObjectSP();
346 }
347 GetValueOffset(iterated_sp);
348 iterated_sp = iterated_sp->GetChildMemberWithName(ConstString("__value_"), true);
349 if (!iterated_sp)
350 {
351 m_tree = NULL;
352 return lldb::ValueObjectSP();
353 }
354 }
355 else
356 {
357 // because of the way our debug info is made, we need to read item 0 first
358 // so that we can cache information used to generate other elements
359 if (m_skip_size == UINT32_MAX)
360 GetChildAtIndex(0);
361 if (m_skip_size == UINT32_MAX)
362 {
363 m_tree = NULL;
364 return lldb::ValueObjectSP();
365 }
366 iterated_sp = iterated_sp->GetSyntheticChildAtOffset(m_skip_size, m_element_type, true);
367 if (!iterated_sp)
368 {
369 m_tree = NULL;
370 return lldb::ValueObjectSP();
371 }
372 }
373 }
374 else
375 {
376 m_tree = NULL;
377 return lldb::ValueObjectSP();
378 }
379 // at this point we have a valid
380 // we need to copy current_sp into a new object otherwise we will end up with all items named __value_
381 DataExtractor data;
Sean Callanan866e91c2014-02-28 22:27:53 +0000382 Error error;
383 iterated_sp->GetData(data, error);
384 if (error.Fail())
385 {
386 m_tree = NULL;
387 return lldb::ValueObjectSP();
388 }
Enrico Granata92373532013-03-19 22:58:48 +0000389 StreamString name;
Deepak Panickal99fbc072014-03-03 15:39:47 +0000390 name.Printf("[%" PRIu64 "]", (uint64_t)idx);
Enrico Granatae29df232014-12-09 19:51:20 +0000391 auto potential_child_sp = CreateValueObjectFromData(name.GetData(), data, m_backend.GetExecutionContextRef(), m_element_type);
Enrico Granata340fa532014-09-12 00:55:37 +0000392 if (potential_child_sp)
393 {
394 switch (potential_child_sp->GetNumChildren())
395 {
396 case 1:
397 {
398 auto child0_sp = potential_child_sp->GetChildAtIndex(0, true);
399 if (child0_sp && child0_sp->GetName() == g___cc)
400 potential_child_sp = child0_sp;
401 break;
402 }
403 case 2:
404 {
405 auto child0_sp = potential_child_sp->GetChildAtIndex(0, true);
406 auto child1_sp = potential_child_sp->GetChildAtIndex(1, true);
407 if (child0_sp && child0_sp->GetName() == g___cc &&
408 child1_sp && child1_sp->GetName() == g___nc)
409 potential_child_sp = child0_sp;
410 break;
411 }
412 }
413 potential_child_sp->SetName(ConstString(name.GetData()));
414 }
415 return (m_children[idx] = potential_child_sp);
Enrico Granata92373532013-03-19 22:58:48 +0000416}
417
418bool
419lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::Update()
420{
421 m_count = UINT32_MAX;
422 m_tree = m_root_node = NULL;
423 m_children.clear();
424 m_tree = m_backend.GetChildMemberWithName(ConstString("__tree_"), true).get();
425 if (!m_tree)
Matt Kopecef143712013-06-03 18:00:07 +0000426 return false;
Enrico Granata92373532013-03-19 22:58:48 +0000427 m_root_node = m_tree->GetChildMemberWithName(ConstString("__begin_node_"), true).get();
428 return false;
429}
430
431bool
432lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::MightHaveChildren ()
433{
434 return true;
435}
436
437size_t
438lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::GetIndexOfChildWithName (const ConstString &name)
439{
440 return ExtractIndexFromString(name.GetCString());
441}
442
443lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::~LibcxxStdMapSyntheticFrontEnd ()
444{}
445
446SyntheticChildrenFrontEnd*
447lldb_private::formatters::LibcxxStdMapSyntheticFrontEndCreator (CXXSyntheticChildren*, lldb::ValueObjectSP valobj_sp)
448{
449 if (!valobj_sp)
450 return NULL;
451 return (new LibcxxStdMapSyntheticFrontEnd(valobj_sp));
452}