Trees | Indices | Help |
|
---|
|
1 import lldb 2 import lldb.formatters.Logger 3 4 # libcxx STL formatters for LLDB 5 # These formatters are based upon the implementation of libc++ that 6 # ships with current releases of OS X - They will not work for other implementations 7 # of the standard C++ library - and they are bound to use the libc++-specific namespace 8 9 # the std::string summary is just an example for your convenience 10 # the actual summary that LLDB uses is C++ code inside the debugger's own core 11 12 # this could probably be made more efficient but since it only reads a handful of bytes at a time 13 # we probably don't need to worry too much about this for the time being15 strval = '' 16 G = F.GetData().uint8 17 for X in range(L): 18 V = G[X] 19 if V == 0: 20 break 21 strval = strval + chr(V % 256) 22 return '"' + strval + '"'23 24 # if we ever care about big-endian, these two functions might need to change26 return True if (value & 1) == 0 else False28 return ((value >> 1) % 256)29 30 # some of the members of libc++ std::string are anonymous or have internal names that convey 31 # no external significance - we access them by index since this saves a name lookup that would add 32 # no information for readers of the code, but when possible try to use meaningful variable names34 logger = lldb.formatters.Logger.Logger() 35 r = valobj.GetChildAtIndex(0) 36 B = r.GetChildAtIndex(0) 37 first = B.GetChildAtIndex(0) 38 D = first.GetChildAtIndex(0) 39 l = D.GetChildAtIndex(0) 40 s = D.GetChildAtIndex(1) 41 D20 = s.GetChildAtIndex(0) 42 size_mode = D20.GetChildAtIndex(0).GetValueAsUnsigned(0) 43 if is_short_string(size_mode): 44 size = extract_short_size(size_mode) 45 return make_string(s.GetChildAtIndex(1),size) 46 else: 47 data_ptr = l.GetChildAtIndex(2) 48 size_vo = l.GetChildAtIndex(1) 49 size = size_vo.GetValueAsUnsigned(0)+1 # the NULL terminator must be accounted for 50 if size <= 1 or size == None: # should never be the case 51 return '""' 52 try: 53 data = data_ptr.GetPointeeData(0,size) 54 except: 55 return '""' 56 error = lldb.SBError() 57 strval = data.GetString(error,0) 58 if error.Fail(): 59 return '<error:' + error.GetCString() + '>' 60 else: 61 return '"' + strval + '"'6264 68132 133 # Just an example: the actual summary is produced by a summary string: size=${svar%#} 13770 logger = lldb.formatters.Logger.Logger() 71 try: 72 start_val = self.start.GetValueAsUnsigned(0) 73 finish_val = self.finish.GetValueAsUnsigned(0) 74 # Before a vector has been constructed, it will contain bad values 75 # so we really need to be careful about the length we return since 76 # unitialized data can cause us to return a huge number. We need 77 # to also check for any of the start, finish or end of storage values 78 # being zero (NULL). If any are, then this vector has not been 79 # initialized yet and we should return zero 80 81 # Make sure nothing is NULL 82 if start_val == 0 or finish_val == 0: 83 return 0 84 # Make sure start is less than finish 85 if start_val >= finish_val: 86 return 0 87 88 num_children = (finish_val-start_val) 89 if (num_children % self.data_size) != 0: 90 return 0 91 else: 92 num_children = num_children/self.data_size 93 return num_children 94 except: 95 return 0;9698 logger = lldb.formatters.Logger.Logger() 99 try: 100 return int(name.lstrip('[').rstrip(']')) 101 except: 102 return -1103105 logger = lldb.formatters.Logger.Logger() 106 logger >> "Retrieving child " + str(index) 107 if index < 0: 108 return None; 109 if index >= self.num_children(): 110 return None; 111 try: 112 offset = index * self.data_size 113 return self.start.CreateChildAtOffset('['+str(index)+']',offset,self.data_type) 114 except: 115 return None116118 logger = lldb.formatters.Logger.Logger() 119 try: 120 self.start = self.valobj.GetChildMemberWithName('__begin_') 121 self.finish = self.valobj.GetChildMemberWithName('__end_') 122 # the purpose of this field is unclear, but it is the only field whose type is clearly T* for a vector<T> 123 # if this ends up not being correct, we can use the APIs to get at template arguments 124 data_type_finder = self.valobj.GetChildMemberWithName('__end_cap_').GetChildMemberWithName('__first_') 125 self.data_type = data_type_finder.GetType().GetPointeeType() 126 self.data_size = self.data_type.GetByteSize() 127 except: 128 pass129139 143168145 logger = lldb.formatters.Logger.Logger() 146 return stdlist_entry(self.entry.GetChildMemberWithName('__next_'))147149 logger = lldb.formatters.Logger.Logger() 150 return stdlist_entry(self.entry.GetChildMemberWithName('__prev_'))151 155 159 163 164 next = property(_next_impl,None) 165 value = property(_value_impl,None) 166 is_null = property(_isnull_impl,None) 167 sbvalue = property(_sbvalue_impl,None)170206 207172 logger = lldb.formatters.Logger.Logger() 173 if node.is_null: 174 return None 175 return node.next176178 logger = lldb.formatters.Logger.Logger() 179 self.node = stdlist_entry(node) # we convert the SBValue to an internal node object on entry180182 logger = lldb.formatters.Logger.Logger() 183 return self.node.sbvalue # and return the SBValue back on exit184186 logger = lldb.formatters.Logger.Logger() 187 node = self.increment_node(self.node) 188 if node != None and node.sbvalue.IsValid() and not(node.is_null): 189 self.node = node 190 return self.value() 191 else: 192 return None193213 217 221 222 # Floyd's cyle-finding algorithm 223 # try to detect if this list has a loop328 329 330 # Just an example: the actual summary is produced by a summary string: size=${svar%#} 334 335 # a tree node - this class makes the syntax in the actual iterator nicer to read and maintain225 global _list_uses_loop_detector 226 logger = lldb.formatters.Logger.Logger() 227 if _list_uses_loop_detector == False: 228 logger >> "Asked not to use loop detection" 229 return False 230 slow = stdlist_entry(self.head) 231 fast1 = stdlist_entry(self.head) 232 fast2 = stdlist_entry(self.head) 233 while slow.next.value != self.node_address: 234 slow_value = slow.value 235 fast1 = fast2.next 236 fast2 = fast1.next 237 if fast1.value == slow_value or fast2.value == slow_value: 238 return True 239 slow = slow.next 240 return False241243 global _list_capping_size 244 logger = lldb.formatters.Logger.Logger() 245 if self.count == None: 246 self.count = self.num_children_impl() 247 if self.count > _list_capping_size: 248 self.count = _list_capping_size 249 return self.count250252 global _list_capping_size 253 logger = lldb.formatters.Logger.Logger() 254 try: 255 next_val = self.head.GetValueAsUnsigned(0) 256 prev_val = self.tail.GetValueAsUnsigned(0) 257 # After a std::list has been initialized, both next and prev will be non-NULL 258 if next_val == 0 or prev_val == 0: 259 return 0 260 if next_val == self.node_address: 261 return 0 262 if next_val == prev_val: 263 return 1 264 if self.has_loop(): 265 return 0 266 size = 2 267 current = stdlist_entry(self.head) 268 while current.next.value != self.node_address: 269 size = size + 1 270 current = current.next 271 if size > _list_capping_size: 272 return _list_capping_size 273 return (size - 1) 274 except: 275 return 0;276278 logger = lldb.formatters.Logger.Logger() 279 try: 280 return int(name.lstrip('[').rstrip(']')) 281 except: 282 return -1283285 logger = lldb.formatters.Logger.Logger() 286 logger >> "Fetching child " + str(index) 287 if index < 0: 288 return None; 289 if index >= self.num_children(): 290 return None; 291 try: 292 current = stdlist_iterator(self.head) 293 current = current.advance(index) 294 # we do not return __value_ because then all our children would be named __value_ 295 # we need to make a copy of __value__ with the right name - unfortunate 296 obj = current.GetChildMemberWithName('__value_') 297 obj_data = obj.GetData() 298 return self.valobj.CreateValueFromData('[' + str(index) + ']',obj_data,self.data_type) 299 except: 300 return None301303 logger = lldb.formatters.Logger.Logger() 304 list_type = self.valobj.GetType().GetUnqualifiedType() 305 if list_type.IsReferenceType(): 306 list_type = list_type.GetDereferencedType() 307 if list_type.GetNumberOfTemplateArguments() > 0: 308 data_type = list_type.GetTemplateArgumentType(0) 309 else: 310 data_type = None 311 return data_type312314 logger = lldb.formatters.Logger.Logger() 315 self.count = None 316 try: 317 impl = self.valobj.GetChildMemberWithName('__end_') 318 self.node_address = self.valobj.AddressOf().GetValueAsUnsigned(0) 319 self.head = impl.GetChildMemberWithName('__next_') 320 self.tail = impl.GetChildMemberWithName('__prev_') 321 self.data_type = self.extract_type() 322 self.data_size = self.data_type.GetByteSize() 323 except: 324 pass325371 372 # a Python implementation of the tree iterator used by libc++338 logger = lldb.formatters.Logger.Logger() 339 return stdmap_iterator_node(self.node.GetChildMemberWithName("__left_"))340342 logger = lldb.formatters.Logger.Logger() 343 return stdmap_iterator_node(self.node.GetChildMemberWithName("__right_"))344346 logger = lldb.formatters.Logger.Logger() 347 return stdmap_iterator_node(self.node.GetChildMemberWithName("__parent_"))348 352 356 360 364 365 left = property(_left_impl,None) 366 right = property(_right_impl,None) 367 parent = property(_parent_impl,None) 368 value = property(_value_impl,None) 369 is_null = property(_null_impl,None) 370 sbvalue = property(_sbvalue_impl,None)374448376 logger = lldb.formatters.Logger.Logger() 377 steps = 0 378 if x.is_null: 379 return None 380 while (not x.left.is_null): 381 x = x.left 382 steps += 1 383 if steps > self.max_count: 384 logger >> "Returning None - we overflowed" 385 return None 386 return x387389 logger = lldb.formatters.Logger.Logger() 390 if x.is_null: 391 return None 392 while (not x.right.is_null): 393 x = x.right 394 return x395397 logger = lldb.formatters.Logger.Logger() 398 if x.is_null: 399 return None 400 return True if x.value == x.parent.left.value else False401403 logger = lldb.formatters.Logger.Logger() 404 if node.is_null: 405 return None 406 if not node.right.is_null: 407 return self.tree_min(node.right) 408 steps = 0 409 while (not self.tree_is_left_child(node)): 410 steps += 1 411 if steps > self.max_count: 412 logger >> "Returning None - we overflowed" 413 return None 414 node = node.parent 415 return node.parent416418 logger = lldb.formatters.Logger.Logger() 419 self.node = stdmap_iterator_node(node) # we convert the SBValue to an internal node object on entry 420 self.max_count = max_count421423 logger = lldb.formatters.Logger.Logger() 424 return self.node.sbvalue # and return the SBValue back on exit425427 logger = lldb.formatters.Logger.Logger() 428 node = self.increment_node(self.node) 429 if node != None and node.sbvalue.IsValid() and not(node.is_null): 430 self.node = node 431 return self.value() 432 else: 433 return None434450577 578 # Just an example: the actual summary is produced by a summary string: size=${svar%#} 582452 logger = lldb.formatters.Logger.Logger() 453 self.valobj = valobj; 454 self.pointer_size = self.valobj.GetProcess().GetAddressByteSize() 455 self.count = None456458 logger = lldb.formatters.Logger.Logger() 459 self.count = None 460 try: 461 # we will set this to True if we find out that discovering a node in the map takes more steps than the overall size of the RB tree 462 # if this gets set to True, then we will merrily return None for any child from that moment on 463 self.garbage = False 464 self.tree = self.valobj.GetChildMemberWithName('__tree_') 465 self.root_node = self.tree.GetChildMemberWithName('__begin_node_') 466 # this data is either lazily-calculated, or cannot be inferred at this moment 467 # we still need to mark it as None, meaning "please set me ASAP" 468 self.data_type = None 469 self.data_size = None 470 self.skip_size = None 471 except: 472 pass473475 global _map_capping_size 476 logger = lldb.formatters.Logger.Logger() 477 if self.count == None: 478 self.count = self.num_children_impl() 479 if self.count > _map_capping_size: 480 self.count = _map_capping_size 481 return self.count482484 logger = lldb.formatters.Logger.Logger() 485 try: 486 return self.valobj.GetChildMemberWithName('__tree_').GetChildMemberWithName('__pair3_').GetChildMemberWithName('__first_').GetValueAsUnsigned() 487 except: 488 return 0;489 492494 logger = lldb.formatters.Logger.Logger() 495 if self.data_type == None or self.data_size == None: 496 if self.num_children() == 0: 497 return False 498 deref = self.root_node.Dereference() 499 if not(deref.IsValid()): 500 return False 501 value = deref.GetChildMemberWithName('__value_') 502 if not(value.IsValid()): 503 return False 504 self.data_type = value.GetType() 505 self.data_size = self.data_type.GetByteSize() 506 self.skip_size = None 507 return True 508 else: 509 return True510512 logger = lldb.formatters.Logger.Logger() 513 if self.skip_size == None: 514 node_type = node.GetType() 515 fields_count = node_type.GetNumberOfFields() 516 for i in range(fields_count): 517 field = node_type.GetFieldAtIndex(i) 518 if field.GetName() == '__value_': 519 self.skip_size = field.GetOffsetInBytes() 520 break 521 return (self.skip_size != None)522524 logger = lldb.formatters.Logger.Logger() 525 try: 526 return int(name.lstrip('[').rstrip(']')) 527 except: 528 return -1529531 logger = lldb.formatters.Logger.Logger() 532 logger >> "Retrieving child " + str(index) 533 if index < 0: 534 return None 535 if index >= self.num_children(): 536 return None; 537 if self.garbage: 538 logger >> "Returning None since this tree is garbage" 539 return None 540 try: 541 iterator = stdmap_iterator(self.root_node,max_count=self.num_children()) 542 # the debug info for libc++ std::map is such that __begin_node_ has a very nice and useful type 543 # out of which we can grab the information we need - every other node has a less informative 544 # type which omits all value information and only contains housekeeping information for the RB tree 545 # hence, we need to know if we are at a node != 0, so that we can still get at the data 546 need_to_skip = (index > 0) 547 current = iterator.advance(index) 548 if current == None: 549 logger >> "Tree is garbage - returning None" 550 self.garbage = True 551 return None 552 if self.get_data_type(): 553 if not(need_to_skip): 554 current = current.Dereference() 555 obj = current.GetChildMemberWithName('__value_') 556 obj_data = obj.GetData() 557 self.get_value_offset(current) # make sure we have a valid offset for the next items 558 # we do not return __value_ because then we would end up with a child named 559 # __value_ instead of [0] 560 return self.valobj.CreateValueFromData('[' + str(index) + ']',obj_data,self.data_type) 561 else: 562 # FIXME we need to have accessed item 0 before accessing any other item! 563 if self.skip_size == None: 564 logger >> "You asked for item > 0 before asking for item == 0, I will fetch 0 now then retry" 565 if self.get_child_at_index(0): 566 return self.get_child_at_index(index) 567 else: 568 logger >> "item == 0 could not be found. sorry, nothing can be done here." 569 return None 570 return current.CreateChildAtOffset('[' + str(index) + ']',self.skip_size,self.data_type) 571 else: 572 logger >> "Unable to infer data-type - returning None (should mark tree as garbage here?)" 573 return None 574 except Exception as err: 575 logger >> "Hit an exception: " + str(err) 576 return None708 766 767 # we can use two different categories for old and new formatters - type names are different enough that we should make no confusion 768 # talking with libc++ developer: "std::__1::class_name is set in stone until we decide to change the ABI. That shouldn't happen within a 5 year time frame"585 logger = lldb.formatters.Logger.Logger() 586 logger.write("init") 587 self.valobj = valobj 588 self.pointer_size = self.valobj.GetProcess().GetAddressByteSize() 589 self.count = None 590 try: 591 self.find_block_size() 592 except: 593 self.block_size = -1 594 self.element_size = -1 595 logger.write("block_size=%d, element_size=%d" % (self.block_size, self.element_size))596598 # in order to use the deque we must have the block size, or else 599 # it's impossible to know what memory addresses are valid 600 self.element_type = self.valobj.GetType().GetTemplateArgumentType(0) 601 self.element_size = self.element_type.GetByteSize() 602 # The code says this, but there must be a better way: 603 # template <class _Tp, class _Allocator> 604 # class __deque_base { 605 # static const difference_type __block_size = sizeof(value_type) < 256 ? 4096 / sizeof(value_type) : 16; 606 # } 607 if self.element_size < 256: 608 self.block_size = 4096 / self.element_size 609 else: 610 self.block_size = 16611613 global _deque_capping_size 614 logger = lldb.formatters.Logger.Logger() 615 if self.count is None: 616 return 0 617 return min(self.count, _deque_capping_size)618 621623 logger = lldb.formatters.Logger.Logger() 624 try: 625 return int(name.lstrip('[').rstrip(']')) 626 except: 627 return -1628630 logger = lldb.formatters.Logger.Logger() 631 logger.write("Fetching child " + str(index)) 632 if index < 0 or self.count is None: 633 return None; 634 if index >= self.num_children(): 635 return None; 636 try: 637 i, j = divmod(self.start+index, self.block_size) 638 return self.first.CreateValueFromExpression('[' + str(index) + ']', 639 '*(*(%s + %d) + %d)' % (self.first.get_expr_path(), i, j)) 640 except: 641 return None642644 logger = lldb.formatters.Logger.Logger() 645 try: 646 # A deque is effectively a two-dim array, with fixed width. 647 # 'map' contains pointers to the rows of this array. The 648 # full memory area allocated by the deque is delimited 649 # by 'first' and 'end_cap'. However, only a subset of this 650 # memory contains valid data since a deque may have some slack 651 # at the front and back in order to have O(1) insertion at 652 # both ends. The rows in active use are delimited by 653 # 'begin' and 'end'. 654 # 655 # To find the elements that are actually constructed, the 'start' 656 # variable tells which element in this NxM array is the 0th 657 # one, and the 'size' element gives the number of elements 658 # in the deque. 659 count = self.valobj.GetChildMemberWithName('__size_').GetChildMemberWithName('__first_').GetValueAsUnsigned(0) 660 # give up now if we cant access memory reliably 661 if self.block_size < 0: 662 logger.write("block_size < 0") 663 return 664 map_ = self.valobj.GetChildMemberWithName('__map_') 665 start = self.valobj.GetChildMemberWithName('__start_').GetValueAsUnsigned(0) 666 first = map_.GetChildMemberWithName('__first_') 667 map_first = first.GetValueAsUnsigned(0) 668 map_begin = map_.GetChildMemberWithName('__begin_').GetValueAsUnsigned(0) 669 map_end = map_.GetChildMemberWithName('__end_').GetValueAsUnsigned(0) 670 map_endcap= map_.GetChildMemberWithName('__end_cap_').GetChildMemberWithName('__first_').GetValueAsUnsigned(0) 671 # check consistency 672 if not map_first <= map_begin <= map_end <= map_endcap: 673 logger.write("map pointers are not monotonic") 674 return 675 total_rows, junk = divmod(map_endcap - map_first, self.pointer_size) 676 if junk: 677 logger.write("endcap-first doesnt align correctly") 678 return 679 active_rows, junk = divmod(map_end - map_begin, self.pointer_size) 680 if junk: 681 logger.write("end-begin doesnt align correctly") 682 return 683 start_row, junk = divmod(map_begin - map_first, self.pointer_size) 684 if junk: 685 logger.write("begin-first doesnt align correctly") 686 return 687 if not start_row*self.block_size <= start < (start_row+1)*self.block_size: 688 logger.write("0th element must be in the 'begin' row") 689 return 690 end_row = start_row + active_rows 691 if not count: 692 if active_rows: 693 logger.write("empty deque but begin!=end") 694 return 695 elif not (end_row-1)*self.block_size <= start+count < end_row*self.block_size: 696 logger.write("nth element must be before the 'end' row") 697 return 698 logger.write("update success: count=%r, start=%r, first=%r" % (count,start,first)) 699 # if consistent, save all we really need: 700 self.count = count 701 self.start = start 702 self.first = first 703 except: 704 self.count = None 705 self.start = None 706 self.map_first = None 707 self.map_begin = None770 debugger.HandleCommand('type summary add -F libcxx.stdstring_SummaryProvider "std::__1::string" -w libcxx') 771 debugger.HandleCommand('type summary add -F libcxx.stdstring_SummaryProvider "std::__1::basic_string<char, class std::__1::char_traits<char>, class std::__1::allocator<char> >" -w libcxx') 772 debugger.HandleCommand('type synthetic add -l libcxx.stdvector_SynthProvider -x "^(std::__1::)vector<.+>$" -w libcxx') 773 debugger.HandleCommand('type summary add -F libcxx.stdvector_SummaryProvider -e -x "^(std::__1::)vector<.+>$" -w libcxx') 774 debugger.HandleCommand('type synthetic add -l libcxx.stdlist_SynthProvider -x "^(std::__1::)list<.+>$" -w libcxx') 775 debugger.HandleCommand('type summary add -F libcxx.stdlist_SummaryProvider -e -x "^(std::__1::)list<.+>$" -w libcxx') 776 debugger.HandleCommand('type synthetic add -l libcxx.stdmap_SynthProvider -x "^(std::__1::)map<.+> >$" -w libcxx') 777 debugger.HandleCommand('type summary add -F libcxx.stdmap_SummaryProvider -e -x "^(std::__1::)map<.+> >$" -w libcxx') 778 debugger.HandleCommand("type category enable libcxx") 779 debugger.HandleCommand('type synthetic add -l libcxx.stddeque_SynthProvider -x "^(std::__1::)deque<.+>$" -w libcxx') 780 debugger.HandleCommand('type synthetic add -l libcxx.stdsharedptr_SynthProvider -x "^(std::__1::)shared_ptr<.+>$" -w libcxx') 781 # turns out the structs look the same, so weak_ptr can be handled the same! 782 debugger.HandleCommand('type synthetic add -l libcxx.stdsharedptr_SynthProvider -x "^(std::__1::)weak_ptr<.+>$" -w libcxx')783 784 _map_capping_size = 255 785 _list_capping_size = 255 786 _list_uses_loop_detector = True 787 _deque_capping_size = 255 788
Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0.1 on Fri Jul 19 13:22:38 2013 | http://epydoc.sourceforge.net |