簡體   English   中英

在Python腳本中控制的GDB中添加斷點命令列表

[英]Adding breakpoint command lists in GDB controlled from Python script

我正在使用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腳本的斷點。 有沒有辦法做到這一點,或者我錯過了一些更容易和更明顯的自動執行斷點特定命令的工具?

可以使用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

另請參見: 如何編寫gdb腳本(使用python)? 示例添加斷點,運行,我們遇到了什么斷點?

我認為這可能是一種更好的方法,而不是使用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子類化為添加“句柄”例程,而不是在循環內進行相等性檢查。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM