我如何从 lldb python API 访问 C 数组浮点值
how do i access C array float values from lldb python API
我是新手 python scripting in lldb。我正在调试 C 代码。
我想将 C 浮点数组的内容复制到 lldb 脚本中的 python 变量中。最终目标是像 here 使用 gdb 和 python.
那样绘制内容数组
我正在调试的程序中的 C 数组声明为 float* buffer;
在 lldb python 脚本中,如果我这样做:buf = lldb.frame.FindVariable ("buffer")
buf 仅包含非解引用指针 "buffer'"(数组的地址)。
如何访问数组的实际值(取消引用)并将它们复制到 python 变量中? gdb gdb_numpy.to_array. I looked at SBValue.h 并尝试了 GetChildAtIndex 方法但没有成功。
问题是 float * 不是数组,它是指向 float 的指针。它可能指向一个连续分配的浮点数数组,或者一个单独的,lldb 无法分辨。
所以它的 SBValue 不会是 N children 的数组,其中 N 是您为数组分配的元素数。它将有一个值——指针值——和一个 child——指针指向的值。
但是,使用 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
实际上:
x[i]= float(buf.GetChildAtIndex(i,1,1).GetValue())
Returns 索引 i 处的 buf 值
@JimIngham 的回答非常好。下面的示例说明了一些其他有用的命令和 Jim 的回答。目的是强调如何处理衰减为指针的 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')
我是新手 python scripting in lldb。我正在调试 C 代码。
我想将 C 浮点数组的内容复制到 lldb 脚本中的 python 变量中。最终目标是像 here 使用 gdb 和 python.
那样绘制内容数组我正在调试的程序中的 C 数组声明为 float* buffer;
在 lldb python 脚本中,如果我这样做:buf = lldb.frame.FindVariable ("buffer")
buf 仅包含非解引用指针 "buffer'"(数组的地址)。
如何访问数组的实际值(取消引用)并将它们复制到 python 变量中? gdb gdb_numpy.to_array. I looked at SBValue.h 并尝试了 GetChildAtIndex 方法但没有成功。
问题是 float * 不是数组,它是指向 float 的指针。它可能指向一个连续分配的浮点数数组,或者一个单独的,lldb 无法分辨。
所以它的 SBValue 不会是 N children 的数组,其中 N 是您为数组分配的元素数。它将有一个值——指针值——和一个 child——指针指向的值。
但是,使用 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
实际上:
x[i]= float(buf.GetChildAtIndex(i,1,1).GetValue())
Returns 索引 i 处的 buf 值
@JimIngham 的回答非常好。下面的示例说明了一些其他有用的命令和 Jim 的回答。目的是强调如何处理衰减为指针的 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')