我如何从 lldb python API 访问 C 数组浮点值

问题描述 投票:0回答:4

我是在 lldb 中使用 python 脚本的新手。我正在调试 C 代码。

我想将 C 浮点数组的内容复制到 lldb 脚本中的 python 变量中。最终目标是使用 gdb 和 python 绘制内容数组,就像

here那样。

我正在调试的程序中的C数组被声明为

float* buffer;



在 lldb python 脚本中,如果我这样做:

buf = lldb.frame.FindVariable ("buffer")

 buf 仅包含非解引用指针“buffer”(数组的地址)。

如何访问数组的实际值(解除引用)并将它们复制到 python 变量中? gdb 有

gdb_numpy.to_array。我查看了 SBValue.h 并尝试了 GetChildAtIndex 方法,但没有成功。

python c lldb
4个回答
3
投票
问题是 float * 不是数组,它是指向 float 的指针。它可能指向一个连续分配的浮点数组,也可能指向单个浮点数组,lldb 无法判断。

因此它的 SBValue 不会是一个包含 N 个子级的数组,其中 N 是您为该数组分配的元素数量。它将有一个值(指针值)和一个子项(指针指向的值)。

但是,使用 Python API 遍历连续数组非常简单:

获取您的指针:

>>> float_ptr = lldb.frame.FindVariable("float_ptr")

为了方便起见,存储元素的类型:

>>> float_type = float_ptr.GetType().GetPointeeType()

现在遍历数组打印出元素(我在这个例子中制作了 10 个浮点数):

>>> for i in range (0,9): ... offset = float_ptr.GetValueAsUnsigned() + i * float_type.GetByteSize() ... val = lldb.target.CreateValueFromAddress("temp", lldb.SBAddress(offset, lldb.target), float_type) ... print val ... (float) temp = 1 (float) temp = 2 (float) temp = 3 (float) temp = 4 (float) temp = 5 (float) temp = 6 (float) temp = 7 (float) temp = 8 (float) temp = 9
    

0
投票
@JimIngham 的回答非常好。下面的示例说明了一些其他有用的命令和吉姆的回答。目标是强调如何处理退化为指针的 C 数组。

void foo_void ( float *input ) { printf("Pointer: %p.\n", input); <-- breakpoint here } int main ( void ) { float tiny_array[4]; tiny_array[0] = 1.0; tiny_array[1] = 2.0; tiny_array[2] = 3.0; tiny_array[3] = 4.0; foo_void ( tiny_array ); return 0; }

LLDB-Python

说明:

(lldb) fr v -L 0x00007ffeefbff4c8: (float *) input = 0x00007ffeefbff4f0 (lldb) script Python Interactive Interpreter. To exit, type 'quit()', 'exit()'. >>> ptr = lldb.frame.FindVariable('input') >>> print(ptr.GetValue()) 0x00007ffeefbff4f0 >>> ptr_type = ptr.GetType().GetPointeeType() >>> print(ptr_type) float >>> ptr_size_type = ptr_type.GetByteSize() >>> print(ptr_size_type) 4 >>> for i in range (0, 4): ... offset = ptr.GetValueAsUnsigned() + i * ptr_size_type ... val = lldb.target.CreateValueFromAddress("temp", lldb.SBAddress(offset, lldb.target), ptr_type) ... print(offset, val.GetValue()) ... (140732920755440, '1') (140732920755444, '2') (140732920755448, '3') (140732920755452, '4')
    

0
投票

lldb.frame.EvaluateExpression 方法。它允许简化脚本:

(lldb) script Python Interactive Interpreter. To exit, type 'quit()', 'exit()'. >>> for i in range (0, 4): ... print(lldb.frame.EvaluateExpression('input[%d]' % i)) ...
    

-1
投票
实际上:

x[i]= float(buf.GetChildAtIndex(i,1,1).GetValue())



返回索引 i 处的 buf 值

© www.soinside.com 2019 - 2024. All rights reserved.