一、简介
作为UNIX/Linux下使用广泛的调试器,gdb不仅提供了丰富的命令,还引入了对脚本的支持:一种是对已存在的脚本语言支持,比如python,用户可以直接书写python脚本,由gdb调用python解释器执行;另一种是命令脚本(command file),用户可以在脚本中书写gdb已经提供的或者自定义的gdb命令,再由gdb执行。
二、命令脚本
自定义命令格式如下
define commandName
statement
......
end
自定义命令帮助文档格式如下
document commandName
statement
.......
end
提示:在gdb中执行脚本要使用source命令,例如:“source xxx.gdb”。
三、python脚本
脚本示例:
1 #!/usr/bin/env python 2 from __future__ import with_statement 3 import gdb 4 5 class SaveBreakpointsCommand (gdb.Command): 6 """Save the current breakpoints to a file. 7 This command takes a single argument, a file name. 8 The breakpoints can be restored using the 'source' command.""" 9 10 def __init__ (self): 11 super (SaveBreakpointsCommand, self).__init__ ("save breakpoints", 12 gdb.COMMAND_SUPPORT, 13 gdb.COMPLETE_FILENAME) 14 15 def invoke (self, arg, from_tty): 16 with open (arg, 'w') as f: 17 for bp in gdb.get_breakpoints (): 18 print >> f, "break", bp.get_location (), 19 if bp.get_thread () is not None: 20 print >> f, " thread", bp.get_thread (), 21 if bp.get_condition () is not None: 22 print >> f, " if", bp.get_condition (), 23 print >> f 24 if not bp.is_enabled (): 25 print >> f, "disable $bpnum" 26 # Note: we don't save the ignore count; there doesn't 27 # seem to be much point. 28 commands = bp.get_commands () 29 if commands is not None: 30 print >> f, "commands" 31 # Note that COMMANDS has a trailing newline. 32 print >> f, commands, 33 print >> f, "end" 34 35 SaveBreakpointsCommand ()
运行:
四、脚本加载方式
gdb加载脚本的方式有
autoload方式 #需要把 脚本放置到/usr/share/gdb/auto-load/usr/lib/
目录下
gdb -x script方式
gdb命令source
script方式