python - GDB pretty-print : printing an array when the size is known by the parent struct

标签 python debugging gdb pretty-print gdb-python

我有一个 C++ 结构,如下所示:

struct HeapBlock {
    char* data;
}

struct DataBlock {
    int size;
    HeapBlock hb;
}

它们是框架的一部分,还有其他几个成员、助手等,但这些是重要的部分。我想在 Python GDB pretty-print 中显示它,如下所示:

NAME                  TYPE             VALUE
DataBlock:            DataBlock       "Size 2000 @ 0x445343"
  |--->size           int             2000
  |--->data           HeapBlock       {...}
        |--->[0]      char            0x34
        |--->[1]      char            0x45
        ....
        <more values>

到目前为止,我未能将 HeapBlock 显示为单独的子项。我已经成功地滥用迭代器来生成:

NAME                  TYPE             VALUE
DataBlock:            DataBlock      
  |--->size           int              2000
  |--->[0]            char             0x34
  |--->[1]            char             0x45
   ....
   <more values>

这是通过从 DataBlockPrinterchildren() 返回的迭代器返回第一个结果中的 db["size"] 来完成的> 方法,然后从 db["hb"]["data"] 获取下一个 size 结果。

我还尝试为 HeapBlocks 使用单独的打印机,但问题是 HeapBlock 不知道它有多大:存储在父级 (DataBlock),因此 HeapBlock 打印机也不知道何时停止迭代。

size字段作为DataBlock的一部分在这里打印时,是否可以将size字段获取到HeapBlock pretty-print ?

最佳答案

我找到了另一种方法

#convert to vector
class DataBlock:
  def iter(self):
    p=self.v['p']
    emtp=p.type.target().unqualified().strip_typedefs()
    pv=emtp.vector(self.cnt-1).pointer()#convert to vector,the actual type is char (*) __attribute__ (vector_size(self.cnt)))
    yield('p',p.cast(pv))
#We register this type
def regCls(v):
    if str(v.type).find(") __attribute__ ((vector_size")>0:
      return _py_vector(v)
def regMyPP():gdb.pretty_printers.append(regCls) 
#Then parse this type out
class _py_vector:
  def __init__(self,v):self.v=v
  def tp(self):
    s=str(self.v.type)
    return gdb.lookup_type(s[:s.find('(')-1]).pointer()#eg.char
  def sz(self):
    s=str(self.v.type)
    st=s.find('vector_size(')+12
    ed=s.find(')',st)
    return int(s[st:ed])#Get it's size
  def to_string(self): return self.v.cast(self.tp()).lazy_string(length=self.sz())
  def display_hint(self): return 'string' 

关于python - GDB pretty-print : printing an array when the size is known by the parent struct,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26471651/

相关文章:

python - 将换行符和 XML 标签替换为 ','

python - 为什么我无法执行存储在环境变量中的 shellcode?

c - 如何将参数传递给 GDB 脚本?

具有外部中断条件的 Python 生成器

python - 如何在没有窗口的情况下将图像直接传输到屏幕?

python - Django 中的 py 文件的 ValueError : Incorrect timezone setting while migrating manage.

php - 什么是 PHP 的 var_dump() 的 Python 等价物?

reactjs - 如何在 React Native 中使用断点进行调试

c# - 如何在 C# 中拦截调试信息(Debugview 样式)?

linux - 为什么在 gdb 中使用 `info proc mappings` 时同一个共享库出现多次?