1

我正在使用 python 围绕 gdb 开发一个包装器。基本上,我只是希望能够预先检测到一些设置烦恼,并能够运行单个命令来调用 gdb,而不是每次都必须记住一个巨大的字符串。

也就是说,我正在使用两种情况。第一个工作正常,通过创建一个新进程并附加到它来调用 gdb。这是我的代码:

def spawnNewProcessInGDB():
    global gObjDir, gGDBProcess;
    from subprocess import Popen
    from os.path import join
    import subprocess
    binLoc = join(gObjDir, 'dist');
    binLoc = join(binLoc, 'bin');
    binLoc = join(binLoc, 'mycommand')

    profileDir = join(gObjDir, '..')
    profileDir = join(profileDir, 'trash-profile')

    try:
        gGDBProcess = Popen(['gdb', '--args', binLoc, '-profile', profileDir], cwd=gObjDir)
        gGDBProcess.wait()
    except KeyboardInterrupt:
        # Send a termination signal to the GDB process, if it's running
        promptAndTerminate(gGDBProcess)

现在,如果用户在它运行时按下CTRL-C,它就会中断(即,它会将 CTRL-C 转发到 GDB)。这是我想要的行为。

第二种情况稍微复杂一些。可能是我已经在我的系统上运行了这个程序,它崩溃了,但被抓住了。在这种情况下,我希望能够使用 gdb 连接到它以获取堆栈跟踪(或者我可能已经在运行它,我现在只想连接到已经在内存中的进程)。

作为一个方便的功能,我创建了一个镜像函数,它将使用 gdb 连接到正在运行的进程:

def connectToProcess(procNum):
    global gObjDir, gGDBProcess
    from subprocess import Popen
    import subprocess
    import signal

    print("Connecting to mycommand process number " + str(procNum) + "...")

    try:
        gGDBProcess = Popen(['gdb', '-p', procNum], cwd=gObjDir)
        gGDBProcess.wait()
    except KeyboardInterrupt:
            promptAndTerminate(gGDBProcess)

同样,这似乎按预期工作。CTRL-C它启动 gdb,我可以设置断点,运行程序等。唯一的问题是,如果我在程序运行时按下它,它不会转发到 gdb。相反,它立即跳转到promptAndTerminate().

我想知道是否有人能明白为什么会发生这种情况——这两个调用对subprocess.Popen()我来说似乎相同,尽管一个调用 gdb 以不同的模式运行。

我也尝试用subprocess.Popen()以下内容替换调用:

gGDBProcess = Popen(['gdb', '-p', procNum], cwd=gObjDir, stdin=subprocess.PIPE)

但这也会导致不良结果,因为它实际上并没有与子 gdb 进程进行任何通信(例如,如果我在c从 gdb 连接中断后输入以启动程序再次运行,它不会做任何事情) . 同样,当我键入CTRL-C.

任何帮助,将不胜感激!

4

0 回答 0