pretty-print.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. import gdb.printing
  2. import re
  3. def create_iter(item, end, idx):
  4. return vectorPrinter._iterator(item, end, idx)
  5. class vectorPrinter:
  6. class _iterator:
  7. def __init__(self, item, end, idx):
  8. self.item = item
  9. self.end = end
  10. self.size = self.end - self.item
  11. self.idx = idx
  12. def __iter__(self):
  13. return self
  14. def __next__(self):
  15. if self.item >= self.end:
  16. raise StopIteration
  17. key = '[%d]' % self.idx
  18. iter = self.item.dereference()
  19. self.item += 1
  20. self.idx += 1
  21. return key, iter
  22. def __init__(self, val):
  23. self.val = val
  24. def to_string(self):
  25. return "std::vector of size %d, capacity %d" % (self.val['m_size'], self.val['m_capacity'])
  26. def display_hint(self):
  27. return 'array'
  28. def children(self):
  29. if self.val['m_size'] == 0:
  30. return [ ('<vector of size 0>', '') ]
  31. return self._iterator(self.val['m_data'], self.val['m_data'] + self.val['m_size'], 0)
  32. def _leftmost(node):
  33. ret = node
  34. while ret['left'] != 0:
  35. ret = ret['left'].dereference()
  36. return ret
  37. def _next(node):
  38. if node['right']:
  39. return _leftmost(node['right'].dereference())
  40. else:
  41. if node['parent'] == 0:
  42. return None
  43. parent = node['parent'].dereference()
  44. if parent['left'] == node.address:
  45. return parent
  46. ret = node
  47. while True:
  48. ret = ret['parent'].dereference()
  49. if ret['parent'] == 0:
  50. return None
  51. if ret['parent'].dereference()['left'] == ret.address:
  52. break
  53. return ret['parent'].dereference()
  54. class rbtreePrinter:
  55. def __init__(self, type, val):
  56. self.type = type
  57. self.val = val['tree']
  58. def to_string(self):
  59. return "%s of size %d" % (self.type, self.val['_size'])
  60. def display_hint(self):
  61. return 'array'
  62. def children(self):
  63. yield 'root', self.val['root']
  64. if self.val['root'] == 0:
  65. return
  66. yield 'size', self.val['_size']
  67. nd = _leftmost(self.val['root'].dereference())
  68. i = 0
  69. while True:
  70. yield "[%d]" % i, nd['value']
  71. nd = _next(nd)
  72. i += 1
  73. if nd == None:
  74. break
  75. class stringPrinter:
  76. def __init__(self, val):
  77. self.val = val
  78. def to_string(self):
  79. return self.val['m_data']
  80. def display_hint(self):
  81. return 'string'
  82. def children(self):
  83. return
  84. yield 'str', self.val['m_data']
  85. if self.val['m_data'] == 0:
  86. return
  87. yield 'length', self.val['m_size'] - 1
  88. ptr = self.val['m_data']
  89. i = 0
  90. while ptr.dereference() != 0:
  91. yield '[%d]' % i, ptr.dereference()
  92. ptr += 1
  93. i += 1
  94. yield '[%d]' % i, 0
  95. class listPrinter:
  96. def __init__(self, val):
  97. self.val: gdb.Field = val
  98. self.type: gdb.Type = val.type
  99. this_type = self.type.unqualified().strip_typedefs()
  100. if this_type.tag == None:
  101. this_type = this_type.target()
  102. self.value_node_type = gdb.lookup_type(this_type.tag + '::node').pointer()
  103. def to_string(self):
  104. if self.type.tag == None and self.val == 0:
  105. return 'nullptr of std::list'
  106. return "std::list of size %d" % self.val['m_size']
  107. def display_hint(self):
  108. return 'array'
  109. def children(self):
  110. if self.type.tag == None and self.val == 0:
  111. return
  112. head = self.val['m_head']
  113. yield 'head', head.address
  114. node = head['next']
  115. idx = 0
  116. while node != head.address:
  117. nodeval = node.reinterpret_cast(self.value_node_type)
  118. yield '[%d]' % idx, nodeval['value']
  119. idx += 1
  120. node = node['next']
  121. class listIteratorPrinter:
  122. def __init__(self, val):
  123. self.val = val
  124. this_type: gdb.Type = val.type
  125. this_type = this_type.unqualified().strip_typedefs()
  126. if this_type.tag == None:
  127. this_type = this_type.target()
  128. type_tag: str = this_type.tag
  129. type_tag = type_tag[:type_tag.rfind('::')]
  130. self.value_node_type = gdb.lookup_type(type_tag + '::node').pointer()
  131. def children(self):
  132. yield 'addr', self.val['p']
  133. if self.val['p'] == 0:
  134. return
  135. nodeptr = self.val['p'].cast(self.value_node_type)
  136. yield 'value', nodeptr['value']
  137. class rbtreeIteratorPrinter:
  138. def __init__(self, val):
  139. self.val = val
  140. def children(self):
  141. yield 'addr', self.val['p']
  142. if self.val['p'] == 0:
  143. return
  144. yield 'value', self.val['p']['value']
  145. class vectorIteratorPrinter:
  146. def __init__(self, val):
  147. self.val = val
  148. def children(self):
  149. yield 'value', self.val['m_ptr'].dereference()
  150. class pairPrinter:
  151. def __init__(self, val):
  152. self.val = val
  153. def children(self):
  154. yield 'first', self.val['first']
  155. yield 'second', self.val['second']
  156. class tuplePrinter:
  157. def __init__(self, val):
  158. self.val = val
  159. def children(self):
  160. i = 0
  161. try:
  162. cur = self.val
  163. while True:
  164. yield '<%d>' % i, cur['val']
  165. i += 1
  166. cur = cur['next']
  167. except Exception:
  168. if i == 0:
  169. yield 'tuple of size 0', ''
  170. class functionPrinter:
  171. def __init__(self, val):
  172. self.val = val
  173. def to_string(self):
  174. return self.val.type.tag
  175. def children(self):
  176. print(self.val['_data'].type)
  177. yield 'function data', self.val['_data']
  178. class referenceWrapperPrinter:
  179. def __init__(self, val):
  180. self.val = val
  181. def to_string(self):
  182. return "std::reference_wrapper to %x" % self.val['_ptr']
  183. def children(self):
  184. yield 'addr', self.val['_ptr'].cast(gdb.lookup_type('void').pointer())
  185. yield 'reference', self.val['_ptr']
  186. def build_pretty_printer(val):
  187. type = val.type
  188. if type.code == gdb.TYPE_CODE_REF:
  189. type = type.target()
  190. if type.code == gdb.TYPE_CODE_PTR:
  191. type = type.target()
  192. type = type.unqualified().strip_typedefs()
  193. typename = type.tag
  194. if typename == None:
  195. return None
  196. if re.compile(r"^std::pair<.*, .*>$").match(typename):
  197. return pairPrinter(val)
  198. if re.compile(r"^std::tuple<.*>$").match(typename):
  199. return tuplePrinter(val)
  200. if re.compile(r"^std::function<.*>$").match(typename):
  201. return functionPrinter(val)
  202. if re.compile(r"^std::reference_wrapper<.*>$").match(typename):
  203. return referenceWrapperPrinter(val)
  204. # if re.compile(r"^std::list<.*, .*>::node$").match(typename):
  205. # return None
  206. if re.compile(r"^std::list<.*, .*>::_iterator<.*?>$").match(typename):
  207. return listIteratorPrinter(val)
  208. if re.compile(r"^std::vector<.*, .*>::_iterator<.*?>$").match(typename):
  209. return vectorIteratorPrinter(val)
  210. if re.compile(r"^std::list<.*, .*>$").match(typename):
  211. return listPrinter(val)
  212. if re.compile(r"^std::vector<.*, .*>$").match(typename):
  213. return vectorPrinter(val)
  214. if re.compile(r"^std::map<.*, .*, .*, .*>$").match(typename):
  215. return rbtreePrinter("std::map", val)
  216. if re.compile(r"^std::set<.*, .*, .*>$").match(typename):
  217. return rbtreePrinter("std::set", val)
  218. if re.compile(r"^std::impl::rbtree<.*, .*, .*>::_iterator<.*?>$").match(typename):
  219. return rbtreeIteratorPrinter(val)
  220. if re.compile(r"^types::string<.*>$").match(typename):
  221. return stringPrinter(val)
  222. return None
  223. gdb.pretty_printers.append(build_pretty_printer)