Skip to content

Add a pretty printer for small mode SmallBitVectors. #29014

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions utils/lldb/lldbSwiftDataFormatters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""
LLDB Formatters for LLVM data types for use in the swift project.

Load into LLDB with 'command script import /path/to/lldbDataFormatters.py'
"""

import sys


def __lldb_init_module(debugger, internal_dict):
tName = 'lldbSwiftDataFormatters.SmallBitVectorSummaryProvider'
debugger.HandleCommand('type summary add -w llvm '
'-F %s -x "^llvm::SmallBitVector$"' % tName)


def SmallBitVectorSummaryProvider(valobj, internal_dict):
underlyingValue = valobj.GetChildMemberWithName('X').GetValueAsUnsigned()
numBaseBits = 32
is64Bit = sys.maxsize > 2**32
if is64Bit:
numBaseBits = 64
smallNumRawBits = numBaseBits - 1
smallNumSizeBits = None
if numBaseBits == 32:
smallNumSizeBits = 5
elif numBaseBits == 64:
smallNumSizeBits = 6
else:
smallNumSizeBits = smallNumRawBits
smallNumDataBits = smallNumRawBits - smallNumSizeBits

# If our underlying value is not small, print we can not dump large values.
isSmallMask = 1
if (underlyingValue & isSmallMask) == 0:
return '<can not read large SmallBitVector>'

smallRawBits = underlyingValue >> 1
smallSize = smallRawBits >> smallNumDataBits
bits = smallRawBits & ((1 << (smallSize + 1)) - 1)
res = "["
for i in reversed(range(0, smallSize)):
if bool(bits & (1 << i)):
res += '1'
else:
res += '0'
res += "]"
return res
12 changes: 12 additions & 0 deletions utils/lldb/lldbToolBox.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
LLVM_REPO = os.path.join(REPO_BASE, "llvm")
LLVM_DATAFORMATTER_PATH = os.path.join(LLVM_REPO, "utils",
"lldbDataFormatters.py")
SWIFT_DATAFORMATTER_PATH = os.path.join(SWIFT_REPO, "utils",
"lldb", "lldbSwiftDataFormatters.py")


def import_llvm_dataformatters(debugger):
Expand All @@ -33,6 +35,15 @@ def import_llvm_dataformatters(debugger):
print("Loaded LLVM data formatters.")


def import_swift_dataformatters(debugger):
if not os.access(SWIFT_DATAFORMATTER_PATH, os.F_OK):
print("WARNING! Could not find Swift data formatters!")
return
cmd = 'command script import {}'.format(SWIFT_DATAFORMATTER_PATH)
debugger.HandleCommand(cmd)
print("Loaded Swift data formatters.")


VIEWCFG_PATH = os.path.join(SWIFT_REPO, "utils", "viewcfg")
BLOCKIFYASM_PATH = os.path.join(SWIFT_REPO, "utils", "dev-scripts",
"blockifyasm")
Expand Down Expand Up @@ -107,6 +118,7 @@ def sequence(debugger, command, exec_ctx, result, internal_dict):

def __lldb_init_module(debugger, internal_dict):
import_llvm_dataformatters(debugger)
import_swift_dataformatters(debugger)
debugger.HandleCommand('command script add disassemble-asm-cfg '
'-f lldbToolBox.disassemble_asm_cfg')
debugger.HandleCommand('command script add disassemble-to-file '
Expand Down