Enrico Granata | 9237353 | 2013-03-19 22:58:48 +0000 | [diff] [blame^] | 1 | //===-- 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 | |
| 22 | using namespace lldb; |
| 23 | using namespace lldb_private; |
| 24 | using namespace lldb_private::formatters; |
| 25 | |
| 26 | class MapEntry |
| 27 | { |
| 28 | public: |
| 29 | MapEntry () {} |
| 30 | MapEntry (ValueObjectSP entry_sp) : m_entry_sp(entry_sp) {} |
| 31 | MapEntry (const MapEntry& rhs) : m_entry_sp(rhs.m_entry_sp) {} |
| 32 | MapEntry (ValueObject* entry) : m_entry_sp(entry ? entry->GetSP() : ValueObjectSP()) {} |
| 33 | |
| 34 | ValueObjectSP |
| 35 | left () |
| 36 | { |
| 37 | if (!m_entry_sp) |
| 38 | return m_entry_sp; |
| 39 | return m_entry_sp->GetChildMemberWithName(ConstString("__left_"), true); |
| 40 | } |
| 41 | |
| 42 | ValueObjectSP |
| 43 | right () |
| 44 | { |
| 45 | if (!m_entry_sp) |
| 46 | return m_entry_sp; |
| 47 | return m_entry_sp->GetChildMemberWithName(ConstString("__right_"), true); |
| 48 | } |
| 49 | |
| 50 | ValueObjectSP |
| 51 | parent () |
| 52 | { |
| 53 | if (!m_entry_sp) |
| 54 | return m_entry_sp; |
| 55 | return m_entry_sp->GetChildMemberWithName(ConstString("__parent_"), true); |
| 56 | } |
| 57 | |
| 58 | uint64_t |
| 59 | value () |
| 60 | { |
| 61 | if (!m_entry_sp) |
| 62 | return 0; |
| 63 | return m_entry_sp->GetValueAsUnsigned(0); |
| 64 | } |
| 65 | |
| 66 | bool |
| 67 | null() |
| 68 | { |
| 69 | return (value() == 0); |
| 70 | } |
| 71 | |
| 72 | ValueObjectSP |
| 73 | GetEntry () |
| 74 | { |
| 75 | return m_entry_sp; |
| 76 | } |
| 77 | |
| 78 | void |
| 79 | SetEntry (ValueObjectSP entry) |
| 80 | { |
| 81 | m_entry_sp = entry; |
| 82 | } |
| 83 | |
| 84 | bool |
| 85 | operator == (const MapEntry& rhs) const |
| 86 | { |
| 87 | return (rhs.m_entry_sp.get() == m_entry_sp.get()); |
| 88 | } |
| 89 | |
| 90 | private: |
| 91 | ValueObjectSP m_entry_sp; |
| 92 | }; |
| 93 | |
| 94 | class MapIterator |
| 95 | { |
| 96 | public: |
| 97 | MapIterator () {} |
| 98 | MapIterator (MapEntry entry, size_t depth = 0) : m_entry(entry), m_max_depth(depth) {} |
| 99 | MapIterator (ValueObjectSP entry, size_t depth = 0) : m_entry(entry), m_max_depth(depth) {} |
| 100 | MapIterator (const MapIterator& rhs) : m_entry(rhs.m_entry),m_max_depth(rhs.m_max_depth) {} |
| 101 | MapIterator (ValueObject* entry, size_t depth = 0) : m_entry(entry), m_max_depth(depth) {} |
| 102 | |
| 103 | ValueObjectSP |
| 104 | value () |
| 105 | { |
| 106 | return m_entry.GetEntry(); |
| 107 | } |
| 108 | |
| 109 | ValueObjectSP |
| 110 | advance (size_t count) |
| 111 | { |
| 112 | if (count == 0) |
| 113 | return m_entry.GetEntry(); |
| 114 | if (count == 1) |
| 115 | { |
| 116 | next (); |
| 117 | return m_entry.GetEntry(); |
| 118 | } |
| 119 | size_t steps = 0; |
| 120 | while (count > 0) |
| 121 | { |
| 122 | next (); |
| 123 | count--; |
| 124 | if (m_entry.null()) |
| 125 | return lldb::ValueObjectSP(); |
| 126 | steps++; |
| 127 | if (steps > m_max_depth) |
| 128 | return lldb::ValueObjectSP(); |
| 129 | } |
| 130 | return m_entry.GetEntry(); |
| 131 | } |
| 132 | protected: |
| 133 | void |
| 134 | next () |
| 135 | { |
| 136 | m_entry.SetEntry(increment(m_entry.GetEntry())); |
| 137 | } |
| 138 | |
| 139 | private: |
| 140 | ValueObjectSP |
| 141 | tree_min (ValueObjectSP x_sp) |
| 142 | { |
| 143 | MapEntry x(x_sp); |
| 144 | if (x.null()) |
| 145 | return ValueObjectSP(); |
| 146 | MapEntry left(x.left()); |
| 147 | size_t steps = 0; |
| 148 | while (left.null() == false) |
| 149 | { |
| 150 | x.SetEntry(left.GetEntry()); |
| 151 | left.SetEntry(x.left()); |
| 152 | steps++; |
| 153 | if (steps > m_max_depth) |
| 154 | return lldb::ValueObjectSP(); |
| 155 | } |
| 156 | return x.GetEntry(); |
| 157 | } |
| 158 | |
| 159 | ValueObjectSP |
| 160 | tree_max (ValueObjectSP x_sp) |
| 161 | { |
| 162 | MapEntry x(x_sp); |
| 163 | if (x.null()) |
| 164 | return ValueObjectSP(); |
| 165 | MapEntry right(x.right()); |
| 166 | size_t steps = 0; |
| 167 | while (right.null() == false) |
| 168 | { |
| 169 | x.SetEntry(right.GetEntry()); |
| 170 | right.SetEntry(x.right()); |
| 171 | steps++; |
| 172 | if (steps > m_max_depth) |
| 173 | return lldb::ValueObjectSP(); |
| 174 | } |
| 175 | return x.GetEntry(); |
| 176 | } |
| 177 | |
| 178 | bool |
| 179 | is_left_child (ValueObjectSP x_sp) |
| 180 | { |
| 181 | MapEntry x(x_sp); |
| 182 | if (x.null()) |
| 183 | return false; |
| 184 | MapEntry rhs(x.parent()); |
| 185 | rhs.SetEntry(rhs.left()); |
| 186 | return x.value() == rhs.value(); |
| 187 | } |
| 188 | |
| 189 | ValueObjectSP |
| 190 | increment (ValueObjectSP x_sp) |
| 191 | { |
| 192 | MapEntry node(x_sp); |
| 193 | if (node.null()) |
| 194 | return ValueObjectSP(); |
| 195 | MapEntry right(node.right()); |
| 196 | if (right.null() == false) |
| 197 | return tree_min(right.GetEntry()); |
| 198 | size_t steps = 0; |
| 199 | while (!is_left_child(node.GetEntry())) |
| 200 | { |
| 201 | node.SetEntry(node.parent()); |
| 202 | steps++; |
| 203 | if (steps > m_max_depth) |
| 204 | return lldb::ValueObjectSP(); |
| 205 | } |
| 206 | return node.parent(); |
| 207 | } |
| 208 | |
| 209 | MapEntry m_entry; |
| 210 | size_t m_max_depth; |
| 211 | }; |
| 212 | |
| 213 | lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::LibcxxStdMapSyntheticFrontEnd (lldb::ValueObjectSP valobj_sp) : |
| 214 | SyntheticChildrenFrontEnd(*valobj_sp.get()), |
| 215 | m_tree(NULL), |
| 216 | m_root_node(NULL), |
| 217 | m_element_type(), |
| 218 | m_element_size(0), |
| 219 | m_skip_size(UINT32_MAX), |
| 220 | m_count(UINT32_MAX), |
| 221 | m_children() |
| 222 | { |
| 223 | if (valobj_sp) |
| 224 | Update(); |
| 225 | } |
| 226 | |
| 227 | size_t |
| 228 | lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::CalculateNumChildren () |
| 229 | { |
| 230 | if (m_count != UINT32_MAX) |
| 231 | return m_count; |
| 232 | if (m_tree == NULL) |
| 233 | return 0; |
| 234 | ValueObjectSP m_item(m_tree->GetChildMemberWithName(ConstString("__pair3_"), true)); |
| 235 | if (!m_item) |
| 236 | return 0; |
| 237 | m_item = m_item->GetChildMemberWithName(ConstString("__first_"), true); |
| 238 | if (!m_item) |
| 239 | return 0; |
| 240 | m_count = m_item->GetValueAsUnsigned(0); |
| 241 | return m_count; |
| 242 | } |
| 243 | |
| 244 | bool |
| 245 | lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::GetDataType() |
| 246 | { |
| 247 | if (m_element_type.GetOpaqueQualType() && m_element_type.GetASTContext()) |
| 248 | return true; |
| 249 | m_element_type.Clear(); |
| 250 | ValueObjectSP deref; |
| 251 | Error error; |
| 252 | deref = m_root_node->Dereference(error); |
| 253 | if (!deref || error.Fail()) |
| 254 | return false; |
| 255 | deref = deref->GetChildMemberWithName(ConstString("__value_"), true); |
| 256 | if (!deref) |
| 257 | return false; |
| 258 | m_element_type.SetClangType(deref->GetClangAST(), deref->GetClangType()); |
| 259 | m_element_size = m_element_type.GetTypeByteSize(); |
| 260 | return true; |
| 261 | } |
| 262 | |
| 263 | void |
| 264 | lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::GetValueOffset (const lldb::ValueObjectSP& node) |
| 265 | { |
| 266 | if (m_skip_size != UINT32_MAX) |
| 267 | return; |
| 268 | if (!node) |
| 269 | return; |
| 270 | ClangASTType node_type(node->GetClangAST(),node->GetClangType()); |
| 271 | uint64_t bit_offset; |
| 272 | if (ClangASTContext::GetIndexOfFieldWithName(node->GetClangAST(),node->GetClangType(),"__value_",NULL,&bit_offset) == UINT32_MAX) |
| 273 | return; |
| 274 | m_skip_size = bit_offset / 8u; |
| 275 | } |
| 276 | |
| 277 | lldb::ValueObjectSP |
| 278 | lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::GetChildAtIndex (size_t idx) |
| 279 | { |
| 280 | if (idx >= CalculateNumChildren()) |
| 281 | return lldb::ValueObjectSP(); |
| 282 | if (m_tree == NULL || m_root_node == NULL) |
| 283 | return lldb::ValueObjectSP(); |
| 284 | |
| 285 | auto cached = m_children.find(idx); |
| 286 | if (cached != m_children.end()) |
| 287 | return cached->second; |
| 288 | |
| 289 | bool need_to_skip = (idx > 0); |
| 290 | MapIterator iterator(m_root_node, CalculateNumChildren()); |
| 291 | ValueObjectSP iterated_sp(iterator.advance(idx)); |
| 292 | if (iterated_sp.get() == NULL) |
| 293 | { |
| 294 | // this tree is garbage - stop |
| 295 | m_tree = NULL; // this will stop all future searches until an Update() happens |
| 296 | return iterated_sp; |
| 297 | } |
| 298 | if (GetDataType()) |
| 299 | { |
| 300 | if (!need_to_skip) |
| 301 | { |
| 302 | Error error; |
| 303 | iterated_sp = iterated_sp->Dereference(error); |
| 304 | if (!iterated_sp || error.Fail()) |
| 305 | { |
| 306 | m_tree = NULL; |
| 307 | return lldb::ValueObjectSP(); |
| 308 | } |
| 309 | GetValueOffset(iterated_sp); |
| 310 | iterated_sp = iterated_sp->GetChildMemberWithName(ConstString("__value_"), true); |
| 311 | if (!iterated_sp) |
| 312 | { |
| 313 | m_tree = NULL; |
| 314 | return lldb::ValueObjectSP(); |
| 315 | } |
| 316 | } |
| 317 | else |
| 318 | { |
| 319 | // because of the way our debug info is made, we need to read item 0 first |
| 320 | // so that we can cache information used to generate other elements |
| 321 | if (m_skip_size == UINT32_MAX) |
| 322 | GetChildAtIndex(0); |
| 323 | if (m_skip_size == UINT32_MAX) |
| 324 | { |
| 325 | m_tree = NULL; |
| 326 | return lldb::ValueObjectSP(); |
| 327 | } |
| 328 | iterated_sp = iterated_sp->GetSyntheticChildAtOffset(m_skip_size, m_element_type, true); |
| 329 | if (!iterated_sp) |
| 330 | { |
| 331 | m_tree = NULL; |
| 332 | return lldb::ValueObjectSP(); |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | else |
| 337 | { |
| 338 | m_tree = NULL; |
| 339 | return lldb::ValueObjectSP(); |
| 340 | } |
| 341 | // at this point we have a valid |
| 342 | // we need to copy current_sp into a new object otherwise we will end up with all items named __value_ |
| 343 | DataExtractor data; |
| 344 | iterated_sp->GetData(data); |
| 345 | StreamString name; |
| 346 | name.Printf("[%zu]",idx); |
| 347 | return (m_children[idx] = ValueObject::CreateValueObjectFromData(name.GetData(), data, m_backend.GetExecutionContextRef(), m_element_type)); |
| 348 | } |
| 349 | |
| 350 | bool |
| 351 | lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::Update() |
| 352 | { |
| 353 | m_count = UINT32_MAX; |
| 354 | m_tree = m_root_node = NULL; |
| 355 | m_children.clear(); |
| 356 | m_tree = m_backend.GetChildMemberWithName(ConstString("__tree_"), true).get(); |
| 357 | if (!m_tree) |
| 358 | return NULL; |
| 359 | m_root_node = m_tree->GetChildMemberWithName(ConstString("__begin_node_"), true).get(); |
| 360 | return false; |
| 361 | } |
| 362 | |
| 363 | bool |
| 364 | lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::MightHaveChildren () |
| 365 | { |
| 366 | return true; |
| 367 | } |
| 368 | |
| 369 | size_t |
| 370 | lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::GetIndexOfChildWithName (const ConstString &name) |
| 371 | { |
| 372 | return ExtractIndexFromString(name.GetCString()); |
| 373 | } |
| 374 | |
| 375 | lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::~LibcxxStdMapSyntheticFrontEnd () |
| 376 | {} |
| 377 | |
| 378 | SyntheticChildrenFrontEnd* |
| 379 | lldb_private::formatters::LibcxxStdMapSyntheticFrontEndCreator (CXXSyntheticChildren*, lldb::ValueObjectSP valobj_sp) |
| 380 | { |
| 381 | if (!valobj_sp) |
| 382 | return NULL; |
| 383 | return (new LibcxxStdMapSyntheticFrontEnd(valobj_sp)); |
| 384 | } |