更新
Decimal
在 Xcode 10.0 中,如果是 a 中的值Dictionary<String: Decimal>
(并且可能在其他情况下),有一个 lldb 错误会使此答案打印错误的值。请参阅此问答和Swift 错误报告 SR-8989。该错误已由 Xcode 11(可能更早)修复。
原来的
您可以通过安装NSDecimal
将. 这称为类型摘要脚本,并记录在lldb 文档的此页面上的“PYTHON SCRIPTING”下。Foundation.Decimal
NSDecimal
使用类型摘要脚本的一个优点是它不涉及在目标进程中运行代码,这对于某些目标可能很重要。
另一个优点是 Xcode 调试器的变量视图似乎更可靠地使用类型摘要脚本而不是使用hypercrypt 的答案中看到的摘要格式。我对摘要格式有疑问,但类型摘要脚本可以可靠地工作。
如果没有类型摘要脚本(或其他自定义),Xcode 会显示如下NSDecimal
(或 Swift Decimal
):

使用类型摘要脚本, Xcode 显示如下:

设置类型摘要脚本涉及两个步骤:
将脚本(如下所示)保存在某个文件中。我把它保存在~/.../lldb/Decimal.py
.
添加命令以~/.lldbinit
加载脚本。该命令应如下所示:
command script import ~/.../lldb/Decimal.py
将路径更改为您存储脚本的位置。
这是脚本。我也把它保存在这个要点中。
# 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"')