Created
October 10, 2018 22:13
-
-
Save mayoff/4070ec1c7efee19b34865179083546bb to your computer and use it in GitHub Desktop.
Decimal / NSDecimal support for lldb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Decimal / NSDecimal support for lldb | |
# | |
# Put this file somewhere, e.g. ~/.../lldb/Decimal.py | |
# Then add this line to ~/.lldbinit: | |
# command script import ~/.../lldb/Decimal.py | |
import lldb | |
def stringForDecimal(sbValue, internal_dict): | |
from decimal import Decimal, getcontext | |
sbData = sbValue.GetData() | |
if not sbData.IsValid(): | |
raise Exception('unable to get data: ' + sbError.GetCString()) | |
if sbData.GetByteSize() != 20: | |
raise Exception('expected data to be 20 bytes but found ' + repr(sbData.GetByteSize())) | |
sbError = lldb.SBError() | |
exponent = sbData.GetSignedInt8(sbError, 0) | |
if sbError.Fail(): | |
raise Exception('unable to read exponent byte: ' + sbError.GetCString()) | |
flags = sbData.GetUnsignedInt8(sbError, 1) | |
if sbError.Fail(): | |
raise Exception('unable to read flags byte: ' + sbError.GetCString()) | |
length = flags & 0xf | |
isNegative = (flags & 0x10) != 0 | |
if length == 0 and isNegative: | |
return 'NaN' | |
if length == 0: | |
return '0' | |
getcontext().prec = 200 | |
value = Decimal(0) | |
scale = Decimal(1) | |
for i in range(length): | |
digit = sbData.GetUnsignedInt16(sbError, 4 + 2 * i) | |
if sbError.Fail(): | |
raise Exception('unable to read memory: ' + sbError.GetCString()) | |
value += scale * Decimal(digit) | |
scale *= 65536 | |
value = value.scaleb(exponent) | |
if isNegative: | |
value = -value | |
return str(value) | |
def __lldb_init_module(debugger, internal_dict): | |
print('registering Decimal type summaries') | |
debugger.HandleCommand('type summary add Foundation.Decimal -F "' + __name__ + '.stringForDecimal"') | |
debugger.HandleCommand('type summary add NSDecimal -F "' + __name__ + '.stringForDecimal"') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment