python - 在 SIGNAL 之后继续使用 gdb 中的 python 脚本

标签 python 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)

诀窍是稍后禁用调度程序锁定,这是避免并发问题所必需的,但如果没有处理程序正在运行,它会导致死锁。

关于python - 在 SIGNAL 之后继续使用 gdb 中的 python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25410568/

相关文章:

python - 如何从python中的fft信号中找到感兴趣频率的bin号?

pointers - 汇编中的星号指针 (I32/x86)

qt - 如何使用来自 Qt Creator 的 GDB 进行远程调试?

python - 在函数中打印返回值

Python:没有名为 contextlib 的模块?

c - 汇编:C编程中变量的内存地址

linux - 设置 debug-file-directory 后再次搜索符号

c++ - 为什么我在检查核心转储时进入 gdb "Cannot access memory at address 0x..."?

python - 解析多个文件(相同格式)在Python中使用 `for`循环

python - pandas 和 fuzzywuzzy 匹配百分比未正确