我正在尝试使用 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)
诀窍是稍后禁用调度程序锁定,这是避免并发问题所必需的,但如果没有处理程序正在运行,它会导致死锁。
关于python - 在 SIGNAL 之后继续使用 gdb 中的 python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25410568/