GDB-Python脚本:迭代C/C++结构域的任何样本

min*_*hua 13 c c++ python scripting gdb

新的GDB-Python脚本API看起来非常强大,应该非常有用.但是,编写一个有用的脚本来迭代C或C++结构中的字段并非易事.有没有人知道一些确切的样品呢?

提前致谢.


更新最终样本:替换_print_fields()早期样本.

    if l.type.code == gdb.TYPE_CODE_STRUCT:
        print "Found a struct  %s " % n
        #self._print_fields(n, t)
        self._print_deep_items(n, t, l)
    else:
        print "Found no struct"

def _print_deep_items (self, n_, type_, instance_):
    for fld in type_.fields():
        fn = fld.name
        ft = fld.type
        fv = instance_[fn]
        if fv.type.code == gdb.TYPE_CODE_STRUCT:
            print "  Found a sub struct  %s " % fn
            self._print_deep_items(fn, ft, fv)
        else:
            print "    Field %s " % fn, " type %s " % ft.tag, " value %s " % fv
Run Code Online (Sandbox Code Playgroud)

并输出:

  variable s1   type S1
Found a struct  s1
    Field v1   type None   value 0
    Field v2   type None   value 0
  Found a sub struct  v3
    Field w3   type None   value 0
Run Code Online (Sandbox Code Playgroud)

使用第一个示例进行更新:获得以下示例代码.这不是最佳选择,因为它在编写字符串字段名称后对每个字段进行查找.abarnert正在展示一种有前途和优雅的方法,工作代码在上面的最终更新部分进行了更新.

import gdb
class PrintGList(gdb.Command):
    """print fields of a struct: wzd struct_object

Iterate through the fields of a struct, and display
a human-readable form of the objects."""
    def __init__(self):
        gdb.Command.__init__(self, "wzd", gdb.COMMAND_DATA, gdb.COMPLETE_SYMBOL, True)

    def invoke(self, arg, from_tty):

        arg_list = gdb.string_to_argv(arg)
        if len(arg_list) < 1:
            print "usage: wzd struct"
            return

        n = arg_list[0]
        l = gdb.parse_and_eval(arg_list[0])
        m = l.type.tag

        print "  variable %s " % n, " type %s " % m
        try:
            t = gdb.lookup_type(m)
        except RuntimeError, e:
            print "type %s not found" % t
            return

        if l.type.code == gdb.TYPE_CODE_STRUCT:
            print "Found a struct  %s " % n
            self._print_fields(n, t)
        else:
            print "Found no struct"

    def _print_fields(self, n, typeobject):
        print typeobject
        flds = typeobject.fields()
        for x in flds:
            sn = n + "." + x.name
            print "  field %s" % sn, " code %s " % x.type.code, " type %s " % x.type.tag
            if x.type.code == gdb.TYPE_CODE_STRUCT:
                print "Found sub level struct  %s " % sn
                sl = gdb.parse_and_eval(sn)
                sm = sl.type.tag
                st = gdb.lookup_type( sm )
                self._print_fields(sn, x.type)

    def _deep_items (self, type_):
        for k, v in type_.iteritems():
            if k:
                print " k v %s " % k , " %s " % v
            else:
                print "   v    ",      " %s " % v

PrintGList()
Run Code Online (Sandbox Code Playgroud)

要测试的源文件:

struct S2 {        int w3;    };
struct S1 {        int v1, v2;      struct S2 v3; } s1;
int main(int argc, char *argv[]) {   return 0; }
Run Code Online (Sandbox Code Playgroud)

示例输出:

  variable s1   type S1
Found a struct  s1
S1
  field s1.v1  typecode 8   type None
  field s1.v2  typecode 8   type None
  field s1.v3  typecode 3   type S2
Found sub level struct  s1.v3
S2
  field s1.v3.w3  typecode 8   type None
Run Code Online (Sandbox Code Playgroud)

GDB会话获取:source /home/me/testpath/wzdfile.py文件a.out b main r wzd s1 quit

aba*_*ert 8

根据文档,迭代C结构的字段应该是非常简单的:

如果类型是结构或类类型或枚举类型,则可以使用Python字典语法访问该类型的字段.例如,如果some_type是包含gdb.Type结构类型的实例,则可以使用以下命令访问其foo字段:

bar = some_type['foo']
Run Code Online (Sandbox Code Playgroud)

bar将成为一个gdb.Field对象; 请参阅以下Type.fields有关gdb.Field该类描述的方法说明.

您还可以使用显式Type.fields获取字段struct,但是(从7.4开始)您也可以使用常规dict方法,以获取名称/ Field对的列表:

for name, field in foo.type.iteritems():
Run Code Online (Sandbox Code Playgroud)

或者,仅为名称:

for name, field in foo.type.iterkeys():
Run Code Online (Sandbox Code Playgroud)

等等.

这似乎没有在该页面上直接记录,但gdb.types意味着它非常强烈,当它说deep_items:

返回类似于标准gdb.Type.iteritems方法的Python迭代器.

例如,给定此C类型:

struct S {
    int x;
    int y;
};
Run Code Online (Sandbox Code Playgroud)

你可以这样做:

(gdb) python struct_S = my_s.type # or gdb.lookup_type("struct S"), etc.
(gdb) python print struct_S.keys()
{['a', 'b']}
(gdb) python print my_s['a']
0
Run Code Online (Sandbox Code Playgroud)

从快速浏览types.py源代码,看看如何gdb.types.deep_item(type_)实现,这似乎就是它的全部内容.


在gdb 7.4之前,您无法直接将类型视为a dict.也就是说,没有for name in instance_.type:instance_.type.iteritems()等你必须显式调用fields.无论如何,将它们放在一起,这是一个用gdb 7.2迭代结构的所有字段的简单示例:

for field in inst.fields:
    fname = field.name
    ftype = field.type
    fval = inst[fname]
Run Code Online (Sandbox Code Playgroud)

除非你structstruct内心有匿名,否则这不起作用.为此,你需要deep_items(如果在7.2中不存在,你需要查看代码并找出如何自己实现它).

所以,在7.2中并不是那么简单,但非常简单.而且,如果你想要琐碎,只需升级到7.4.

  • @minghua:第二:你不能直接在`instance_`上迭代; 你必须迭代`instance_.type`或`instance_.type.fields()`来获取名称和类型; 然后你可以为每个名字做`instance_ [name]`.让我更新我的答案,包括7.2个例子(虽然我实际上没有7.2,所以这将是未经测试的). (2认同)