該腳本實現了一個新的GDB命令,它採用一個C結構作爲參數。 您可以後class PrintGList
"""print fields of a struct: wzd struct_object
Iterate through the fields of a struct, and display
a human-readable form of the objects."""
從Python的文檔字符串告訴您想要的腳本來實現自定義數據類型的GDB漂亮打印機和改變,當你使用GDB的打印命令得到什麼打印,但是這不是劇本如何迷上。
類名稱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()
測試程序(結構-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)
大家好,..我等待着你的迴應....... – 2013-05-13 05:08:06