在Python脚本中控制的GDB中添加断点命令列表

时间:2014-08-21 16:28:24

标签: python gdb

我正在使用Python通过批处理命令控制GDB。这就是我打电话给GDB的方式:

$ gdb --batch --command=cmd.gdb myprogram

cmd.gdb列表只包含调用Python脚本的行

source cmd.py

cmd.py脚本尝试创建断点和附加命令列表

bp = gdb.Breakpoint("myFunc()") # break at function in myprogram
gdb.execute("commands " + str(bp.number))
# then what? I'd like to at least execute a "continue" on reaching breakpoint...  
gdb.execute("run")

问题是我不知道如何将任何GDB命令附加到Python脚本的断点。有没有办法做到这一点,或者我错过了一些更容易和更明显的自动执行断点特定命令的工具?

2 个答案:

答案 0 :(得分:3)

可以使用GDB 7.7.1中的

def stop

gdb.execute('file a.out', to_string=True)
class MyBreakpoint(gdb.Breakpoint):
    def stop (self):
        gdb.write('MyBreakpoint\n')
        # Continue automatically.
        return False
        # Actually stop.
        return True
MyBreakpoint('main')
gdb.execute('run')

记录于:https://sourceware.org/gdb/onlinedocs/gdb/Breakpoints-In-Python.html#Breakpoints-In-Python

另请参阅:How to script gdb (with python)? Example add breakpoints, run, what breakpoint did we hit?

答案 1 :(得分:0)

我认为这可能是一种更好的方法,而不是使用GDB的“命令列表”工具。

bp1 = gdb.Breakpoint("myFunc()")

# Define handler routines
def stopHandler(stopEvent):
    for b in stopEvent.breakpoints:
        if b == bp1:
            print "myFunc() breakpoint"
        else:
            print "Unknown breakpoint"
    gdb.execute("continue")

# Register event handlers
gdb.events.stop.connect (stopHandler)

gdb.execute("run")

您也可以将gdb.Breakpoint子类化为添加“句柄”例程,而不是在循环内进行相等性检查。