[英]GDB not stopping with "interrupt" command from python script
我一直在扯我的頭發。 我搜索了互聯網,似乎無法找到解決我的問題的方法。 我正在嘗試使用 python 中的 gdb 模塊自動測試一些代碼。 除了停止在后台運行的進程外,我可以執行基本命令並且一切正常。 目前,我在斷點后在后台繼續我的程序:
gdb.execute("c&")
然后我與正在運行的程序進行交互,讀取不同的常量值並從程序中獲取響應。
接下來我需要獲得一塊內存,所以我運行這些命令:
gdb.execute("interrupt") #Pause execution gdb.execute("dump binary memory montiormem.bin 0x0 (&__etext + 4)") #dump memory to file
但是當我運行內存轉儲時,我收到一個錯誤,說在目標運行時無法運行命令,錯誤后運行中斷命令並且目標暫停,然后從 gdb 控制台窗口我可以運行內存傾倒。
我不久前發現了一個類似的問題, 這里似乎沒有回答。
我正在使用python2.7。
我還發現這個鏈接似乎是問題,但沒有跡象表明它是否在我的 gdb 構建中(這似乎不太可能)。
我在編寫一些自動化測試腳本時遇到了同樣的問題。 我注意到的是,“中斷”命令直到當前腳本退出后才會停止應用程序。
不幸的是,這意味着您需要在導致中斷的任何時候對腳本進行分段。
腳本 1:
gdb.execute('c&')
gdb.execute('interrupt')
腳本2:
gdb.execute("dump binary memory montiormem.bin 0x0 (&__etext + 4)")
我使用多線程來解決這個問題:
def post(cmd):
def _callable():
print("exec " + cmd , flush=True)
gdb.execute(cmd)
print("schedule " + cmd , flush=True)
gdb.post_event(_callable)
class ScriptThread (threading.Thread):
def run (self):
while True:
post("echo hello\n")
time.sleep(1)
x = ScriptThread()
x.start()
將其另存為“test_script.py”
使用腳本如下:
gdb
> source test_script.py
注意:您也可以使用管道“source test_script.py”,但您需要保持管道打開。
一旦線程啟動,GDB 將等待線程結束並處理您通過“post_event”函數發送給它的任何命令。 甚至“中斷”!
我遇到了同樣的問題,從谷歌搜索可以看出這是 gdb 的當前限制: interrupt
在批處理模式下根本不起作用(使用--ex
或-x file
指定命令時,或在標准輸入上,或采購從文件),它在實際停止執行之前運行以下命令(插入延遲沒有幫助)。 基於@dwjbosman 的解決方案,這里有一個緊湊的版本,適合使用--ex
參數提供給 gdb,例如:
python import threading, gdb
python threading.Timer(1.0, lambda: gdb.post_event(lambda: gdb.execute("interrupt"))).start()
cont
thread apply all bt full # or whatever you wanted to do
它在 1 秒后安排中斷並恢復程序,然后您可以在主腳本中暫停后做任何您想做的事情。
我遇到了同樣的問題,但是發現如果您嘗試從 python 編寫所有內容,那么這里的其他答案都沒有真正起作用。 我遇到的問題是,當我調用gdb.execute('continue')
時,任何其他 python 線程中的代碼都不會執行。 這似乎是因為gdb 在 continue 命令正在等待程序被中斷時沒有釋放 python GIL 。
我發現實際上對我有用的是:
def delayed_interrupt():
time.sleep(1)
gdb.execute('interrupt')
gdb.post_event(delayed_interrupt)
gdb.execute('continue')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.