lldb_webkit.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. # Copyright (C) 2012 Apple. All rights reserved.
  2. #
  3. # Redistribution and use in source and binary forms, with or without
  4. # modification, are permitted provided that the following conditions
  5. # are met:
  6. # 1. Redistributions of source code must retain the above copyright
  7. # notice, this list of conditions and the following disclaimer.
  8. # 2. Redistributions in binary form must reproduce the above copyright
  9. # notice, this list of conditions and the following disclaimer in the
  10. # documentation and/or other materials provided with the distribution.
  11. #
  12. # THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
  13. # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  14. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  15. # DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  16. # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  17. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  18. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  19. # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  20. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  21. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. """
  23. LLDB Support for WebKit Types
  24. Add the following to your .lldbinit file to add WebKit Type summaries in LLDB and Xcode:
  25. command script import {Path to WebKit Root}/Tools/lldb/lldb_webkit.py
  26. """
  27. import lldb
  28. import struct
  29. def __lldb_init_module(debugger, dict):
  30. debugger.HandleCommand('type summary add --expand -F lldb_webkit.WTFString_SummaryProvider WTF::String')
  31. debugger.HandleCommand('type summary add --expand -F lldb_webkit.WTFStringImpl_SummaryProvider WTF::StringImpl')
  32. debugger.HandleCommand('type summary add --expand -F lldb_webkit.WTFAtomicString_SummaryProvider WTF::AtomicString')
  33. debugger.HandleCommand('type summary add --expand -F lldb_webkit.WTFVector_SummaryProvider -x "WTF::Vector<.+>$"')
  34. debugger.HandleCommand('type summary add --expand -F lldb_webkit.WTFHashTable_SummaryProvider -x "WTF::HashTable<.+>$"')
  35. debugger.HandleCommand('type synthetic add -x "WTF::Vector<.+>$" --python-class lldb_webkit.WTFVectorProvider')
  36. debugger.HandleCommand('type synthetic add -x "WTF::HashTable<.+>$" --python-class lldb_webkit.WTFHashTableProvider')
  37. def WTFString_SummaryProvider(valobj, dict):
  38. provider = WTFStringProvider(valobj, dict)
  39. return "{ length = %d, contents = '%s' }" % (provider.get_length(), provider.to_string())
  40. def WTFStringImpl_SummaryProvider(valobj, dict):
  41. provider = WTFStringImplProvider(valobj, dict)
  42. return "{ length = %d, is8bit = %d, contents = '%s' }" % (provider.get_length(), provider.is_8bit(), provider.to_string())
  43. def WTFAtomicString_SummaryProvider(valobj, dict):
  44. return WTFString_SummaryProvider(valobj.GetChildMemberWithName('m_string'), dict)
  45. def WTFVector_SummaryProvider(valobj, dict):
  46. provider = WTFVectorProvider(valobj, dict)
  47. return "{ size = %d, capacity = %d }" % (provider.size, provider.capacity)
  48. def WTFHashTable_SummaryProvider(valobj, dict):
  49. provider = WTFHashTableProvider(valobj, dict)
  50. return "{ tableSize = %d, keyCount = %d }" % (provider.tableSize(), provider.keyCount())
  51. # FIXME: Provide support for the following types:
  52. # def WTFVector_SummaryProvider(valobj, dict):
  53. # def WTFCString_SummaryProvider(valobj, dict):
  54. # def WebCoreKURLGooglePrivate_SummaryProvider(valobj, dict):
  55. # def WebCoreQualifiedName_SummaryProvider(valobj, dict):
  56. # def JSCIdentifier_SummaryProvider(valobj, dict):
  57. # def JSCJSString_SummaryProvider(valobj, dict):
  58. def guess_string_length(valobj, charSize, error):
  59. if not valobj.GetValue():
  60. return 0
  61. maxLength = 256
  62. pointer = valobj.GetValueAsUnsigned()
  63. contents = valobj.GetProcess().ReadMemory(pointer, maxLength * charSize, lldb.SBError())
  64. format = 'B' if charSize == 1 else 'H'
  65. for i in xrange(0, maxLength):
  66. if not struct.unpack_from(format, contents, i * charSize)[0]:
  67. return i
  68. return maxLength
  69. def ustring_to_string(valobj, error, length=None):
  70. if length is None:
  71. length = guess_string_length(valobj, 2, error)
  72. else:
  73. length = int(length)
  74. pointer = valobj.GetValueAsUnsigned()
  75. contents = valobj.GetProcess().ReadMemory(pointer, length * 2, lldb.SBError())
  76. # lldb does not (currently) support returning unicode from python summary providers,
  77. # so potentially convert this to ascii by escaping
  78. string = contents.decode('utf16')
  79. try:
  80. return str(string)
  81. except:
  82. return string.encode('unicode_escape')
  83. def lstring_to_string(valobj, error, length=None):
  84. if length is None:
  85. length = guess_string_length(valobj, 1, error)
  86. else:
  87. length = int(length)
  88. pointer = valobj.GetValueAsUnsigned()
  89. contents = valobj.GetProcess().ReadMemory(pointer, length, lldb.SBError())
  90. # lldb does not (currently) support returning unicode from python summary providers,
  91. # so potentially convert this to ascii by escaping
  92. string = contents.decode('utf8')
  93. try:
  94. return str(string)
  95. except:
  96. return string.encode('unicode_escape')
  97. class WTFStringImplProvider:
  98. def __init__(self, valobj, dict):
  99. self.valobj = valobj
  100. def get_length(self):
  101. return self.valobj.GetChildMemberWithName('m_length').GetValueAsUnsigned(0)
  102. def get_data8(self):
  103. return self.valobj.GetChildAtIndex(2).GetChildMemberWithName('m_data8')
  104. def get_data16(self):
  105. return self.valobj.GetChildAtIndex(2).GetChildMemberWithName('m_data16')
  106. def to_string(self):
  107. error = lldb.SBError()
  108. if self.is_8bit():
  109. return lstring_to_string(self.get_data8(), error, self.get_length())
  110. return ustring_to_string(self.get_data16(), error, self.get_length())
  111. def is_8bit(self):
  112. # FIXME: find a way to access WTF::StringImpl::s_hashFlag8BitBuffer
  113. return bool(self.valobj.GetChildMemberWithName('m_hashAndFlags').GetValueAsUnsigned(0) \
  114. & 1 << 6)
  115. class WTFStringProvider:
  116. def __init__(self, valobj, dict):
  117. self.valobj = valobj
  118. def stringimpl(self):
  119. impl_ptr = self.valobj.GetChildMemberWithName('m_impl').GetChildMemberWithName('m_ptr')
  120. return WTFStringImplProvider(impl_ptr, dict)
  121. def get_length(self):
  122. impl = self.stringimpl()
  123. if not impl:
  124. return 0
  125. return impl.get_length()
  126. def to_string(self):
  127. impl = self.stringimpl()
  128. if not impl:
  129. return u""
  130. return impl.to_string()
  131. class WTFVectorProvider:
  132. def __init__(self, valobj, internal_dict):
  133. self.valobj = valobj
  134. self.update()
  135. def num_children(self):
  136. return self.size + 3
  137. def get_child_index(self, name):
  138. if name == "m_size":
  139. return self.size
  140. elif name == "m_capacity":
  141. return self.size + 1
  142. elif name == "m_buffer":
  143. return self.size + 2
  144. else:
  145. return int(name.lstrip('[').rstrip(']'))
  146. def get_child_at_index(self, index):
  147. if index == self.size:
  148. return self.valobj.GetChildMemberWithName("m_size")
  149. elif index == self.size + 1:
  150. return self.valobj.GetChildMemberWithName("m_capacity")
  151. elif index == self.size + 2:
  152. return self.buffer
  153. elif index < self.size:
  154. offset = index * self.data_size
  155. child = self.buffer.CreateChildAtOffset('[' + str(index) + ']', offset, self.data_type)
  156. return child
  157. else:
  158. return None
  159. def update(self):
  160. self.buffer = self.valobj.GetChildMemberWithName('m_buffer')
  161. self.size = self.valobj.GetChildMemberWithName('m_size').GetValueAsUnsigned(0)
  162. self.capacity = self.buffer.GetChildMemberWithName('m_capacity').GetValueAsUnsigned(0)
  163. self.data_type = self.buffer.GetType().GetPointeeType()
  164. self.data_size = self.data_type.GetByteSize()
  165. def has_children(self):
  166. return True
  167. class WTFHashTableProvider:
  168. def __init__(self, valobj, internal_dict):
  169. self.valobj = valobj
  170. self.update()
  171. def num_children(self):
  172. return self.tableSize() + 5
  173. def get_child_index(self, name):
  174. if name == "m_table":
  175. return self.tableSize()
  176. elif name == "m_tableSize":
  177. return self.tableSize() + 1
  178. elif name == "m_tableSizeMask":
  179. return self.tableSize() + 2
  180. elif name == "m_keyCount":
  181. return self.tableSize() + 3
  182. elif name == "m_deletedCount":
  183. return self.tableSize() + 4
  184. else:
  185. return int(name.lstrip('[').rstrip(']'))
  186. def get_child_at_index(self, index):
  187. if index == self.tableSize():
  188. return self.valobj.GetChildMemberWithName('m_table')
  189. elif index == self.tableSize() + 1:
  190. return self.valobj.GetChildMemberWithName('m_tableSize')
  191. elif index == self.tableSize() + 2:
  192. return self.valobj.GetChildMemberWithName('m_tableSizeMask')
  193. elif index == self.tableSize() + 3:
  194. return self.valobj.GetChildMemberWithName('m_keyCount')
  195. elif index == self.tableSize() + 4:
  196. return self.valobj.GetChildMemberWithName('m_deletedCount')
  197. elif index < self.tableSize():
  198. table = self.valobj.GetChildMemberWithName('m_table')
  199. return table.CreateChildAtOffset('[' + str(index) + ']', index * self.data_size, self.data_type)
  200. else:
  201. return None
  202. def tableSize(self):
  203. return self.valobj.GetChildMemberWithName('m_tableSize').GetValueAsUnsigned(0)
  204. def keyCount(self):
  205. return self.valobj.GetChildMemberWithName('m_keyCount').GetValueAsUnsigned(0)
  206. def update(self):
  207. self.data_type = self.valobj.GetType().GetTemplateArgumentType(0)
  208. self.data_size = self.data_type.GetByteSize()
  209. def has_children(self):
  210. return True