gdb python:任何人都可以解释我如何使用这篇文章写的脚本? [英] gdb python : Can anyone explain me how to use this script written in this post?
问题描述
如何做一个c代码..?可能吗..?我读过这篇文章。我也想做类似的事情,但我无法使用给定的更新脚本链接
GDB-Python脚本:遍历C / C ++结构字段的任何示例
我遵循以下步骤测试:
我的源代码名称是:test.c和pretty.py
gcc -g test.c
$ b $ p $ g $ c $ g $ c
< code $(gdb)源码pretty.py
(gdb)run
(gdb)print
如何使用这个脚本?
该脚本实现了一个新的GDB命令 wzd 它采用C结构作为参数。
您可以在 class PrintGList
结构体的打印字段:wzd struct_object
遍历结构体的字段,并显示
a对象的可读形式。
您希望脚本为自定义数据类型实现GDB漂亮打印机,并更改使用GDB的打印时打印的内容
命令不支持脚本。 起源于在glib库中打印链接列表的脚本。再次复制和粘贴代码罢工;)我已经修复了一些小错误并清除了以下代码( wzd.py ):$ b返回t.code == gdb.TYPE_CODE_STRUCT
$ b $ class WZD(gdb .Command):
'''结构体的打印字段:wzd struct_object
遍历一个结构体的字段,并显示
a这些对象的可读形式。 ''
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:
printusage:wzd struct
return
$ b $ = arg_list [0]
l = gdb.parse_and_eval(arg_list [0])
( t,m)=(l.type,l.type.tag)
printvariable%s%n,type%s%t
if l .type.code == gdb.TYPE_CODE_STRUCT:
printFound a struct%s%n
self._print_fields(n,t)
else:
printFound no结构体
$ b sn = n +。 + x.name $ b $ if if _type_is_container(x.type):
tag_msg =',tag:%r'%(x.type.tag,)
else:
tag_msg = ''
print'字段%r类型%s(代码:%s%s)'%(sn,x.type,x.type.code,tag_msg)
if _type_is_container(x.type) :
printFound sub level struct%s%sn
sl = gdb.parse_and_eval(sn)
sm = sl.type.tag
st = sl.type
self._print_fields(sn,x.type)
$ b $ def _deep_items(self,type_):
for k,v in type_.iteritems():
if k:
printkv%s%k,%s%v
else:
printv,%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();
返回0;
示例GDB会话:
$ echo 1 2 x> in
$ gdb -q -x wzd.py struct-read
< ...>
(gdb)运行< in
< ...>
编程接收到的信号SIGTRAP,跟踪/断点陷阱。
main()在struct-read.c中:25
25
$ b $(gdb)wzd s
变量s类型struct S
找到一个struct s
struct S
's.t'类型struct T(代码:3,tag:'T')
找到子级struct st
struct T
字段'stx'类型int(代码:8)
字段'sty'类型int(代码:8)
字段's.b'类型char(代码:8)
How to do it for a c code..? Is it possible..? I read this post. I also want to do similar things but i am not able to use the given updated script at link GDB-Python scripting: any samples iterating through C/C++ struct fields
I followed the following steps to test : my source code name was : test.c and pretty.py
gcc -g test.c
gdb test
(gdb) source pretty.py
(gdb) run
(gdb) print <stcruct object>
How to use this script?
That script implements a new GDB command, wzd which takes a C structure as argument.
You can tell from the Python doc string after 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."""
You were expecting the script to implement a GDB pretty printer for a custom data type and change what gets printed when you use GDB's print command but that's not how the script is hooked up.
The class name PrintGList
suggests that code originated from a script that printed the linked lists in the glib library. Copy and paste coding strikes again ;) I've fixed a few minor bugs and cleaned up the code below (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()
Test program (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;
}
Sample GDB session:
$ 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)
这篇关于gdb python:任何人都可以解释我如何使用这篇文章写的脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!