当 SBValue 来自 Swift 字典时,SBData 是错误的
SBData is wrong when SBValue comes from a Swift Dictionary
我正在尝试编写一个 Python 函数来格式化 Foundation.Decimal
,以用作类型摘要器。我在 this answer 中发布了它。我还将把它包含在这个答案的底部,以及额外的调试打印。
我现在发现了一个错误,但我不知道这个错误是在我的函数中,还是在 lldb 中,或者可能在 Swift 编译器中。
这是演示错误的文字记录。我在 ~/.lldbinit
中加载了我的类型摘要器,因此 Swift REPL 使用它。
:; xcrun swift
registering Decimal type summaries
Welcome to Apple Swift version 4.2 (swiftlang-1000.11.37.1 clang-1000.11.45.1). Type :help for assistance.
1> import Foundation
2> let dec: Decimal = 7
dec: Decimal = 7
以上,调试器输出中的 7
来自我的类型汇总器并且是正确的。
3> var dict = [String: Decimal]()
dict: [String : Decimal] = 0 key/value pairs
4> dict["x"] = dec
5> dict["x"]
$R0: Decimal? = 7
以上,7
再次来自我的类型摘要器,并且是正确的。
6> dict
$R1: [String : Decimal] = 1 key/value pair {
[0] = {
key = "x"
value = 0
}
}
上面的 0
(在 value = 0
中)来自我的类型摘要,不正确。应该是 7
.
那为什么是零呢?我的 Python 函数被赋予 SBValue
。它在 SBValue
上调用 GetData()
以获得 SBData
。我在函数中添加了调试打印以打印 SBData
中的字节,并打印 sbValue.GetLoadAddress()
的结果。这是带有这些调试打印的记录:
:; xcrun swift
registering Decimal type summaries
Welcome to Apple Swift version 4.2 (swiftlang-1000.11.37.1 clang-1000.11.45.1). Type :help for assistance.
1> import Foundation
2> let dec: Decimal = 7
dec: Decimal = loadAddress: ffffffffffffffff
data: 00 21 00 00 07 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
7
上面我们可以看到加载地址是假的,但是SBData
的字节是正确的(字节1,21
,包含长度和标志;字节4,'07 ', 是有效数字的第一个字节)。
3> var dict = [String: Decimal]()
dict: [String : Decimal] = 0 key/value pairs
4> dict["x"] = dec
5> dict
$R0: [String : Decimal] = 1 key/value pair {
[0] = {
key = "x"
value = loadAddress: ffffffffffffffff
data: 00 00 00 00 00 21 00 00 07 00 00 00 00 00 00 00 00 00 00 00
0
}
}
上面我们可以看到加载地址还是假的,现在SBData
的字节是不正确的。 SBData
仍然包含 20 个字节(Foundation.Decimal
的正确数字,又名 NSDecimal
),但是现在前面插入了四个 00
字节,最后四个字节已被删除。
所以这是我的具体问题:
我是否错误地使用了 lldb API,从而得到了错误的答案?如果是这样,我做错了什么,应该如何纠正?
如果我正确使用了 lldb API,那么这是 lldb 中的错误,还是 Swift 编译器发出了不正确的元数据?我如何找出哪个工具有错误? (因为如果它是其中一个工具中的错误,我想提交错误报告。)
如果它是 lldb 或 Swift 中的一个错误,我该如何解决这个问题,以便当 Decimal
是 [=38= 的一部分时我可以正确地格式化它]?
这是我的类型格式化程序,带有调试打印:
# 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
print(' loadAddress: %x' % sbValue.GetLoadAddress())
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
debugString = ''
for i in range(20):
debugString += ' %02x' % sbData.GetUnsignedInt8(sbError, i)
print(' data:' + debugString)
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"')
这看起来像一个 lldb 错误。请使用 http://bugs.swift.org.
针对 lldb 提交有关此问题的错误
作为背景:Dictionary 案例在您的背后发生了一些神奇的事情。我无法在 REPL 中显示这一点,但是如果您在某些实际代码中将 [String : Decimal] 数组作为局部变量并执行:
(lldb) frame variable --raw dec_array
(Swift.Dictionary<Swift.String, Foundation.Decimal>) dec_array = {
_variantBuffer = native {
native = {
_storage = 0x0000000100d05780 {
Swift._SwiftNativeNSDictionary = {}
bucketCount = {
_value = 2
}
count = {
_value = 1
}
initializedEntries = {
values = {
_rawValue = 0x0000000100d057d0
}
bitCount = {
_value = 2
}
}
keys = {
_rawValue = 0x0000000100d057d8
}
values = {
_rawValue = 0x0000000100d057f8
}
seed = {
0 = {
_value = -5794706384231184310
}
1 = {
_value = 8361200869849021207
}
}
}
}
cocoa = {
cocoaDictionary = 0x00000001000021b0
}
}
}
A swift 字典实际上并不包含任何明显的字典元素,当然也不包含 ivars。所以 lldb 有一个 "Synthetic child provider" 用于 Swift 字典,它构成字典的键和值的 SBValues,它是你的格式化程序正在传递的那些合成子项之一。
这也是加载地址为-1的原因。这真的意味着 "this is a synthetic thing whose data lldb is directly managing, not a thing at an address somewhere in your program." REPL 结果也是如此,它们更像是 lldb 维护的小说。但是如果你查看 Decimal 类型的局部变量,你会看到一个有效的加载地址,因为它存在于内存中的某个地方。
无论如何,很明显,我们正在编写的用于表示字典值的合成子 Decimal 对象没有正确设置数据的开头。有趣的是,如果您制作 [Decimal : String] 字典,则键字段的 SBData 是正确的,并且您的格式化程序可以正常工作。只是价值观不对。
我对以字符串作为值的字典进行了同样的尝试,SBData 看起来是正确的。所以 Decimal 有一些有趣的地方。无论如何,感谢您的关注,请提交错误。
我正在尝试编写一个 Python 函数来格式化 Foundation.Decimal
,以用作类型摘要器。我在 this answer 中发布了它。我还将把它包含在这个答案的底部,以及额外的调试打印。
我现在发现了一个错误,但我不知道这个错误是在我的函数中,还是在 lldb 中,或者可能在 Swift 编译器中。
这是演示错误的文字记录。我在 ~/.lldbinit
中加载了我的类型摘要器,因此 Swift REPL 使用它。
:; xcrun swift
registering Decimal type summaries
Welcome to Apple Swift version 4.2 (swiftlang-1000.11.37.1 clang-1000.11.45.1). Type :help for assistance.
1> import Foundation
2> let dec: Decimal = 7
dec: Decimal = 7
以上,调试器输出中的 7
来自我的类型汇总器并且是正确的。
3> var dict = [String: Decimal]()
dict: [String : Decimal] = 0 key/value pairs
4> dict["x"] = dec
5> dict["x"]
$R0: Decimal? = 7
以上,7
再次来自我的类型摘要器,并且是正确的。
6> dict
$R1: [String : Decimal] = 1 key/value pair {
[0] = {
key = "x"
value = 0
}
}
上面的 0
(在 value = 0
中)来自我的类型摘要,不正确。应该是 7
.
那为什么是零呢?我的 Python 函数被赋予 SBValue
。它在 SBValue
上调用 GetData()
以获得 SBData
。我在函数中添加了调试打印以打印 SBData
中的字节,并打印 sbValue.GetLoadAddress()
的结果。这是带有这些调试打印的记录:
:; xcrun swift
registering Decimal type summaries
Welcome to Apple Swift version 4.2 (swiftlang-1000.11.37.1 clang-1000.11.45.1). Type :help for assistance.
1> import Foundation
2> let dec: Decimal = 7
dec: Decimal = loadAddress: ffffffffffffffff
data: 00 21 00 00 07 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
7
上面我们可以看到加载地址是假的,但是SBData
的字节是正确的(字节1,21
,包含长度和标志;字节4,'07 ', 是有效数字的第一个字节)。
3> var dict = [String: Decimal]()
dict: [String : Decimal] = 0 key/value pairs
4> dict["x"] = dec
5> dict
$R0: [String : Decimal] = 1 key/value pair {
[0] = {
key = "x"
value = loadAddress: ffffffffffffffff
data: 00 00 00 00 00 21 00 00 07 00 00 00 00 00 00 00 00 00 00 00
0
}
}
上面我们可以看到加载地址还是假的,现在SBData
的字节是不正确的。 SBData
仍然包含 20 个字节(Foundation.Decimal
的正确数字,又名 NSDecimal
),但是现在前面插入了四个 00
字节,最后四个字节已被删除。
所以这是我的具体问题:
我是否错误地使用了 lldb API,从而得到了错误的答案?如果是这样,我做错了什么,应该如何纠正?
如果我正确使用了 lldb API,那么这是 lldb 中的错误,还是 Swift 编译器发出了不正确的元数据?我如何找出哪个工具有错误? (因为如果它是其中一个工具中的错误,我想提交错误报告。)
如果它是 lldb 或 Swift 中的一个错误,我该如何解决这个问题,以便当
Decimal
是 [=38= 的一部分时我可以正确地格式化它]?
这是我的类型格式化程序,带有调试打印:
# 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
print(' loadAddress: %x' % sbValue.GetLoadAddress())
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
debugString = ''
for i in range(20):
debugString += ' %02x' % sbData.GetUnsignedInt8(sbError, i)
print(' data:' + debugString)
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"')
这看起来像一个 lldb 错误。请使用 http://bugs.swift.org.
针对 lldb 提交有关此问题的错误作为背景:Dictionary 案例在您的背后发生了一些神奇的事情。我无法在 REPL 中显示这一点,但是如果您在某些实际代码中将 [String : Decimal] 数组作为局部变量并执行:
(lldb) frame variable --raw dec_array
(Swift.Dictionary<Swift.String, Foundation.Decimal>) dec_array = {
_variantBuffer = native {
native = {
_storage = 0x0000000100d05780 {
Swift._SwiftNativeNSDictionary = {}
bucketCount = {
_value = 2
}
count = {
_value = 1
}
initializedEntries = {
values = {
_rawValue = 0x0000000100d057d0
}
bitCount = {
_value = 2
}
}
keys = {
_rawValue = 0x0000000100d057d8
}
values = {
_rawValue = 0x0000000100d057f8
}
seed = {
0 = {
_value = -5794706384231184310
}
1 = {
_value = 8361200869849021207
}
}
}
}
cocoa = {
cocoaDictionary = 0x00000001000021b0
}
}
}
A swift 字典实际上并不包含任何明显的字典元素,当然也不包含 ivars。所以 lldb 有一个 "Synthetic child provider" 用于 Swift 字典,它构成字典的键和值的 SBValues,它是你的格式化程序正在传递的那些合成子项之一。
这也是加载地址为-1的原因。这真的意味着 "this is a synthetic thing whose data lldb is directly managing, not a thing at an address somewhere in your program." REPL 结果也是如此,它们更像是 lldb 维护的小说。但是如果你查看 Decimal 类型的局部变量,你会看到一个有效的加载地址,因为它存在于内存中的某个地方。
无论如何,很明显,我们正在编写的用于表示字典值的合成子 Decimal 对象没有正确设置数据的开头。有趣的是,如果您制作 [Decimal : String] 字典,则键字段的 SBData 是正确的,并且您的格式化程序可以正常工作。只是价值观不对。
我对以字符串作为值的字典进行了同样的尝试,SBData 看起来是正确的。所以 Decimal 有一些有趣的地方。无论如何,感谢您的关注,请提交错误。