【问题标题】:gdb python : Can anyone explain me how to use this script written in this post?gdb python:谁能解释我如何使用这篇文章中写的这个脚本?
【发布时间】:2013-05-10 13:27:35
【问题描述】:

如何为 c 代码执行此操作..?是否可以..?我读了这篇文章。我也想做类似的事情,但我无法在链接中使用给定的更新脚本 GDB-Python scripting: any samples iterating through C/C++ struct fields

我按照以下步骤进行测试: 我的源代码名称是:test.c 和 pretty.py

gcc -g test.c

gdb test

(gdb) source pretty.py

(gdb) run

(gdb) print <stcruct object>

如何使用这个脚本?

【问题讨论】:

  • 大家好,.. 我正在等待您的回复........

标签: python gdb gdb-python


【解决方案1】:

该脚本实现了一个新的 GDB 命令,wzd,它将 C 结构作为参数。 你可以从class PrintGList之后的Python文档字符串中看出@

"""print fields of a struct: wzd struct_object
Iterate through the fields of a struct, and display
a human-readable form of the objects."""

您希望脚本为自定义数据类型实现 GDB 漂亮打印机,并在您使用 GDB 的 print 命令时更改打印的内容,但这不是脚本的连接方式。

类名PrintGList 表明代码源自打印glib 库中的链表的脚本。再次复制和粘贴代码罢工;)我已经修复了一些小错误并清理了下面的代码(wzd.py):

import gdb

def _type_is_container(t):
    return t.code == gdb.TYPE_CODE_STRUCT

class WZD(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])
        (t, m) = (l.type, l.type.tag)

        print "  variable %s " % n, " type %s " % t

        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
            if _type_is_container(x.type):
                tag_msg = ', tag: %r' % (x.type.tag,)
            else:
                tag_msg = ''
            print '  field %r type %s (code: %s%s)' % (sn, x.type, x.type.code, tag_msg)
            if _type_is_container(x.type):
                print "Found sub level struct  %s " % sn
                sl = gdb.parse_and_eval(sn)
                sm = sl.type.tag
                st = sl.type
                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

WZD()

测试程序(struct-read.c):

#include <assert.h>
#include <stdio.h>

/* https://github.com/scottt/debugbreak */
#include <debugbreak/debugbreak.h>

struct T {
    int x, y;
};

struct S {
    struct T t;
    char b;
};

int main()
{
    int r;
    struct S s;
    r = scanf("%d%d%c", &s.t.x, &s.t.y, &s.b);
    assert(r == 3);
    debug_break();

    return 0;
}

示例 GDB 会话:

$ echo 1 2 x > in
$ gdb -q -x wzd.py struct-read
<...>

(gdb) run < in
<...>
Program received signal SIGTRAP, Trace/breakpoint trap.
main () at struct-read.c:25
25  }

(gdb) wzd s
  variable s   type struct S 
Found a struct  s 
struct S
  field 's.t' type struct T (code: 3, tag: 'T')
Found sub level struct  s.t 
struct T
  field 's.t.x' type int (code: 8)
  field 's.t.y' type int (code: 8)
  field 's.b' type char (code: 8)

【讨论】:

  • 非常感谢斯科特。:-)
  • @BaijnathJaiswal,不客气。我想强调一下,上面的 Python 代码写得不是很好,也不是很干净。如果您想使用 Python 和 GDB 处理特定的数据结构,如果遇到问题,请考虑在 SO 上发布另一个问题。
  • 当然..!再次感谢您的回答对我很有帮助。:-)
  • @scottt,我们可以用同样的方式打印所有的全局变量和局部变量(对应的值)吗..?
猜你喜欢
  • 1970-01-01
  • 2015-09-27
  • 1970-01-01
  • 2022-07-19
  • 2021-01-08
  • 2012-02-12
  • 1970-01-01
  • 2022-11-10
  • 2017-03-11
相关资源
最近更新 更多