【问题标题】:how do i access C array float values from lldb python API我如何从 lldb python API 访问 C 数组浮点值
【发布时间】:2016-12-11 17:47:33
【问题描述】:

我是使用python scripting in lldb 的新手。我正在调试 C 代码。

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

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

如果我这样做,在 lldb python 脚本中:buf = lldb.frame.FindVariable ("buffer") buf 仅包含非取消引用指针“缓冲区”(数组的地址)。

如何访问数组的实际值(取消引用)并将它们复制到 python 变量中? gdb 有gdb_numpy.to_array。我查看了SBValue.h 并尝试了 GetChildAtIndex 方法,但没有成功。

【问题讨论】:

    标签: python c lldb


    【解决方案1】:

    问题是 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
    

    【讨论】:

      【解决方案2】:

      @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')
      

      【讨论】:

        【解决方案3】:

        其实:

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

        返回索引 i 处的 buf 值

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-01-21
          • 1970-01-01
          • 2014-12-25
          • 1970-01-01
          • 2017-08-04
          • 1970-01-01
          • 2019-10-16
          相关资源
          最近更新 更多