lldb:将数组显示为十六进制

lldb: Displaying array as hex

在 lldb 中,是否可以显示一个 uint8_t 字节数组以连接的十六进制?

例如,显示 here 描述的 uint256 类型的默认行为是

{
  [14] = '['
  [15] = '\xbf'
  [16] = '('
  [17] = '\xc3'
  [18] = 'O'
  [19] = ':'
  [20] = '^'
  [21] = '3'
  [22] = '*'
  [23] = '\x1f'
  [24] = '\xc7'
  [25] = '\xb2'
  [26] = '\xb7'
  [27] = '<'
  [28] = '\xf1'
  [29] = '\x88'
  [30] = '\x91'
  [31] = '\x0f'
}

by 运行 type summary add --summary-string "${var.data[0-31]%x}" uint256 我可以这样显示:

[0x06,0x22,0x6e,0x46,0x11,0x1a,0x0b,0x59,0xca,0xaf,0x12,0x60,0x43,0xeb,0x5b,0xbf,0x28,0xc3,0x4f,0x3a,0x5e,0x33,0x2a,0x1f,0xc7,0xb2,0xb7,0x3c,0xf1,0x88,0x91,0x0f]

但我想要这样的东西:

0x06226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f

我想不出使用摘要字符串格式化程序来执行此操作的方法。我们为 char[] 做了一些像这样的特殊用途魔法,但它在您不想要的 %s 说明符后面。

但是,编写 python 摘要来执行此操作相当容易。这是类型 uint8_t [32]:

的简单示例
def unvector_32 (valobj,internal_dict):
    str = "0x" 
    for i in range(0,31): 
         str += ("%x"%(valobj.GetChildAtIndex(i).GetValueAsUnsigned())).zfill(2) 
    return str

def __lldb_init_module(debugger, internal_dict):
    debugger.HandleCommand('type category define -e my_formatters')
    debugger.HandleCommand('type summary add -w my_formatters -F %s.unvector_32 "uint8_t [32]" -v'%(__name__))

将它放在某个 .py 文件中(我将所有这些都放在 ~/.lldb 中)并执行:

(lldb) com scr imp ~/.lldb/my_formatters.py

要么在你的 lldb 会话中,要么在你的 ~/.lldbinit 中。然后你会看到:

(lldb) fr v data
(uint8_t [32]) data = 0x01020304050600000000000000000000000000000000000000000000000000
(lldb)

我认为这大致就是您想要的输出。要将其应用于 uint256 class,您可以在添加摘要时将匹配类型更改为 "uint256",并让摘要函数首先使用 GetChildMemberWithName("data") 获取 data 成员,然后打印单个矢量元素。如果您希望格式化程序更通用,您还可以对 uint([0-9]+) 使用正则表达式匹配,然后使用 GetNumChildren 调用获取 data 中的元素数...

顺便说一句,我发现将所有格式化程序放在一个类别中很方便,这样我就可以轻松列出它们 (type summary list -w my_formatters),如果我搞砸了,也可以轻松将它们全部关闭...

关于 SB API 的更多信息在这里:

http://lldb.llvm.org/python_reference/index.html

您可以将 lldb 调试器的格式更改为另一种格式

type format add -f bytes uint8_t

Bytes 要使用的格式

uiint8_t 类型以在

上应用格式

更多详情Link:- https://lldb.llvm.org/use/variable.html