一、簡介
作為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方式
新聞熱點
疑難解答