Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从lldb python API访问C数组浮点值_Python_C_Lldb - Fatal编程技术网

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

如何从lldb python API访问C数组浮点值,python,c,lldb,Python,C,Lldb,我是新来的。我正在调试C代码 我想将浮点C数组的内容复制到lldb脚本中的python变量中。最终目标是像gdb和python一样绘制内容数组 我正在调试的程序中的C数组被声明为float*buffer 在lldb python脚本中,如果我这样做:buf=lldb.frame.FindVariable(“buffer”)buf仅包含非反序列化指针“buffer”(数组的地址) 如何访问数组的实际值(无差异)并将其复制到python变量中?gdb有。我查看并尝试了GetChildAtIndex方

我是新来的。我正在调试C代码

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

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

在lldb python脚本中,如果我这样做:
buf=lldb.frame.FindVariable(“buffer”)
buf仅包含非反序列化指针“buffer”(数组的地址)


如何访问数组的实际值(无差异)并将其复制到python变量中?gdb有。我查看并尝试了GetChildAtIndex方法,但没有成功。

问题在于浮点*不是数组,而是指向浮点的指针。它可能指向一个连续分配的浮点数组,或者一个lldb无法分辨的浮点数组

所以它的SBValue不是N个子数组,其中N是为数组分配的元素数。它将有一个值-指针值-和一个子-指针指向的值

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

获取指针:

>>> float_ptr = lldb.frame.FindVariable("float_ptr")
为方便起见,请存储元素的类型:

>>> float_type = float_ptr.GetType().GetPointeeType()
现在遍历数组打印出元素(在本例中,我制作了10个浮点):

实际上:

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


返回索引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;
}
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) 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')