[英]Continue after SIGNAL with a python script in gdb
我試圖用gdb中的python腳本生成一些關於分段錯誤(和其他信號)的輸出。 該腳本如下所示:
import gdb
def event_handler(event):
gdb.execute("set scheduler-locking on") # this is needed to avoid parallel exec of the handler
gdb.write("\n[ME] SIG " + event.stop_signal)
frame = gdb.selected_frame()
while frame:
gdb.write("\n[ME] FN " + str(frame.name()))
frame = frame.older()
# make sure output goes to a file
gdb.execute("set logging on")
gdb.execute("set logging file gdbout")
gdb.events.stop.connect(event_handler)
問題是我需要在每個分段錯誤上按c和Enter,腳本不會繼續。
如果我做
gdb.execute("continue")
在處理程序中,我得到一個StackOverflow。 我認為這是因為execute()永遠不會返回。 如果我做
handle SIGSEGV nostop
我的處理程序不再被調用。 如何在處理程序后繼續?
好的,我發現了怎么做:
首先,我需要一個可調用的continue命令。 正如Tom所建議的,這將與post_event一起使用:
class Executor:
def __init__(self, cmd):
self.__cmd = cmd
def __call__(self):
gdb.execute(self.__cmd)
這是事件處理程序:
def event_handler(event):
gdb.execute("set scheduler-locking on") # to avoid parallel signals in other threads
gdb.write("\n[ME] SIG " + event.stop_signal)
frame = gdb.selected_frame()
while frame:
gdb.write("\n[ME] FN " + str(frame.name()))
frame = frame.older()
gdb.execute("set scheduler-locking off") # otherwise just this thread is continued, leading to a deadlock
gdb.post_event(Executor("continue")) # and post the continue command to gdb
然后,調用它:
gdb.execute("set logging on")
gdb.execute("set logging file gdbout")
gdb.execute("set pagination off")
gdb.events.stop.connect(event_handler)
訣竅是稍后禁用調度程序鎖定,需要避免並發問題,但如果沒有運行處理程序,則會導致死鎖。
不幸的是,在gdb中仍然沒有一個好的Python API信號。 相反,你必須訴諸黑客。 那就是說,你的方法對我來說似乎很好。
為了處理“繼續”問題,我建議使用gdb.post_event
將事件插入到gdb的事件隊列中。 該事件可以調用continue
命令。 這應該至少避免堆棧溢出問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.