manticore_lldb.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. # to use me add the command to ~/.lldbinit
  2. # command script import "/path/to/manticore/misc/manticore_lldb.py"
  3. # to debug run run directly:
  4. # command script import "/path/to/manticore/misc/manticore_lldb.py" --allow-reload
  5. class VecTraitsSynthProvider:
  6. def __init__(self, valobj, dict):
  7. self.valobj = valobj
  8. self.count = None
  9. def num_children(self):
  10. if self.count is None:
  11. self.count = self.num_children_impl()
  12. return self.count
  13. def num_children_impl(self):
  14. try:
  15. start_val = self.pData.GetValueAsUnsigned(0)
  16. # Make sure nothing is NULL
  17. if start_val == 0:
  18. return 0
  19. return self.iCount
  20. except:
  21. return 0
  22. def get_child_at_index(self, index):
  23. if index < 0:
  24. return None
  25. if index >= self.num_children():
  26. return None
  27. try:
  28. offset = index * self.data_size
  29. return self.pData.CreateChildAtOffset('[' + str(index) + ']', offset, self.data_type)
  30. except:
  31. return None
  32. def update(self):
  33. # preemptively setting this to None - we might end up changing our
  34. # mind later
  35. self.count = None
  36. try:
  37. self.pData = self.valobj.GetChildMemberWithName('m_pData')
  38. self.iCount = self.valobj.GetChildMemberWithName('m_iCount')
  39. self.data_type = self.pData.GetType().GetPointeeType()
  40. self.data_size = self.data_type.GetByteSize()
  41. # if any of these objects is invalid, it means there is no
  42. # point in trying to fetch anything
  43. if self.pData.IsValid() and self.data_type.IsValid():
  44. self.count = self.iCount.GetValueAsUnsigned()
  45. else:
  46. self.count = 0
  47. except:
  48. pass
  49. return True
  50. def get_child_index(self, name):
  51. try:
  52. return int(name.lstrip('[').rstrip(']'))
  53. except:
  54. return -1
  55. def has_children(self):
  56. return True
  57. def VecTraitsSummaryProvider(valobj, internal_dict):
  58. if valobj.IsValid():
  59. content = valobj.GetChildMemberWithName('(content)')
  60. if content.IsValid():
  61. return content.GetSummary()
  62. else:
  63. count = valobj.GetNonSyntheticValue().GetChildMemberWithName('m_iCount').GetValueAsUnsigned()
  64. return '{} elems'.format(str(count))
  65. def VecSummaryProvider(valobj, internal_dict):
  66. if valobj.IsValid():
  67. content = valobj.GetChildMemberWithName('(content)')
  68. if content.IsValid():
  69. return content.GetSummary()
  70. else:
  71. count = valobj.GetNonSyntheticValue().GetChildMemberWithName('m_iCount').GetValueAsUnsigned()
  72. limit = valobj.GetNonSyntheticValue().GetChildMemberWithName('m_iLimit').GetValueAsUnsigned()
  73. return '{} ({}) elems'.format(str(count), str(limit))
  74. def LocatorSummaryProvider(valobj, internal_dict):
  75. if valobj.IsValid():
  76. content = valobj.GetChildMemberWithName('(content)')
  77. if content.IsValid():
  78. return content.GetSummary()
  79. else:
  80. bitoffset = valobj.GetNonSyntheticValue().GetChildMemberWithName('m_iBitOffset').GetValueAsSigned()
  81. bitcount = valobj.GetNonSyntheticValue().GetChildMemberWithName('m_iBitCount').GetValueAsSigned()
  82. type = valobj.GetNonSyntheticValue().GetChildMemberWithName('m_bDynamic').GetValueAsUnsigned()
  83. stype = 'S'
  84. if type:
  85. stype = 'D'
  86. if bitoffset>0:
  87. return '{} {}/{} ({}/{})'.format(stype, str(bitoffset), str(bitcount), str(bitoffset/32), str(bitcount/32))
  88. return '{} {}/{}'.format(stype, str(bitoffset), str(bitcount))
  89. def JsonNodeValue(valobj):
  90. svalue = valobj.GetNonSyntheticValue().GetChildMemberWithName('m_sValue')
  91. type = valobj.GetNonSyntheticValue().GetChildMemberWithName('m_eType').GetValueAsUnsigned()
  92. if type == 0:
  93. return 'EOF'
  94. elif type == 1 or type == 2:
  95. return 'I32({})'.format(
  96. str(valobj.GetNonSyntheticValue().GetChildMemberWithName('m_iValue').GetValueAsSigned()))
  97. elif type == 3:
  98. return 'D({})'.format(str(valobj.GetNonSyntheticValue().GetChildMemberWithName('m_fValue').GetValue()))
  99. elif type == 4:
  100. return 'STR{}'.format(svalue.GetSummary())
  101. elif type == 5:
  102. return 'STRV{}'.format(svalue.GetSummary())
  103. elif type == 6:
  104. return 'I32V{}'.format(svalue.GetSummary())
  105. elif type == 7:
  106. return 'I64V{}'.format(svalue.GetSummary())
  107. elif type == 8:
  108. return 'DV{}'.format(svalue.GetSummary())
  109. elif type == 9:
  110. return 'MV{})'.format(svalue.GetSummary())
  111. elif type == 10:
  112. return 'OBJ{}'.format(svalue.GetSummary())
  113. elif type == 11:
  114. return 'TRUE'
  115. elif type == 12:
  116. return 'FALSE'
  117. elif type == 13:
  118. return 'NULL'
  119. elif type == 14:
  120. return 'ROOT{}'.format(svalue.GetSummary())
  121. return 'UNKNOWN'
  122. def JsonNodeSummaryProvider(valobj, internal_dict):
  123. if valobj.IsValid():
  124. content = valobj.GetChildMemberWithName('(content)')
  125. if content.IsValid():
  126. return content.GetSummary()
  127. else:
  128. svalue = JsonNodeValue(valobj)
  129. sname = valobj.GetNonSyntheticValue().GetChildMemberWithName('m_sName')
  130. inamelen = sname.GetChildMemberWithName('m_iLen').GetValueAsSigned()
  131. if inamelen != 0:
  132. snameval = 'name{}'.format( sname.GetSummary())
  133. else:
  134. snameval = ''
  135. inext = valobj.GetNonSyntheticValue().GetChildMemberWithName('m_iNext').GetValueAsSigned()
  136. if inext>-1:
  137. snext = 'next {}'.format(str(inext))
  138. else:
  139. snext = 'last'
  140. return '{} {}- {}'.format(svalue, snameval, snext)
  141. def __lldb_init_module(debugger, unused):
  142. # look https://github.com/llvm/llvm-project/blob/master/lldb/examples/synthetic/libcxx.py
  143. #Vec-like - VecTraits_T, CSphVector<*, CSphFixedVector<*, CSphTightVector<*
  144. debugger.HandleCommand('type synthetic add -x "VecTraits_T|CSph(Vector|FixedVector|TightVector|SwapVector)<" -l manticore_lldb.VecTraitsSynthProvider')
  145. # summary for Vectraits and FixedVector - num of elems; for usual vector - also limit
  146. debugger.HandleCommand('type summary add -x "VecTraits_T<|CSphFixedVector<" --summary-string "[${var%#}]"')
  147. debugger.HandleCommand('type summary add -x "(sph::Vector_T|CSph(Vector|TightVector|SwapVector))<" --summary-string "[${var%#}] (${var.m_iLimit})"')
  148. #debugger.HandleCommand('type summary add -x "CSphVector<" -F manticore_lldb.VecSummaryProvider')
  149. #CSphString
  150. debugger.HandleCommand('type summary add CSphString --summary-string "${var.m_sValue}"')
  151. debugger.HandleCommand('type summary add StringBuilder_c --summary-string "${var.m_szBuffer} ${var.m_iUsed} (${var.m_iSize})"')
  152. debugger.HandleCommand(
  153. 'type summary add StringBuilder_c::LazyComma_c --summary-string "${var.m_sPrefix} ${var.m_sComma} ${var.m_sSuffix}"')
  154. #std::pair
  155. debugger.HandleCommand('type summary add -x "^std::(__1::)?pair<" --inline-children')
  156. # debugger.HandleCommand('type summary add -x "^std::(__1::)?pair<" --summary-string "${var.first}:${var.second}"')
  157. debugger.HandleCommand('type summary add --inline-children Comma_c')
  158. debugger.HandleCommand('type summary add Str_t --summary-string "${var.first}"')
  159. debugger.HandleCommand('type summary add --inline-children CSphMatch')
  160. # debugger.HandleCommand('type summary add CSphAttrLocator --summary-string "${var.m_bDynamic} ${var.m_iBitOffset}/${var.m_iBitCount} (${var.m_iBitOffset/32}/${var.m_iBitCount/32})"')
  161. debugger.HandleCommand('type summary add -x CSphAttrLocator -F manticore_lldb.LocatorSummaryProvider')
  162. debugger.HandleCommand('type summary add BlobLocator_t --summary-string "(${var.m_iStart}:${var.m_iLen})"')
  163. # debugger.HandleCommand('type summary add CSphColumnInfo --summary-string "${var.m_sName} ${var.m_eAttrType} at (${var.m_tLocator})"')
  164. debugger.HandleCommand('type summary add -x JsonNode_t$ -F manticore_lldb.JsonNodeSummaryProvider')