如何使用 shell = true 使用 Python subprocess.Popen() 将 SIGINT 传递给子进程

2022-01-18 00:00:00 python subprocess popen gdb sigint

问题描述

我目前正在尝试为 GDB 编写 (Python 2.7.3) 一种包装器,这将允许我从脚本输入动态切换到与 GDB 的交互式通信.

I am currently trying to write (Python 2.7.3) kind of a wrapper for GDB, which will allow me to dynamically switch from scripted input to interactive communication with GDB.

到目前为止我使用

self.process = subprocess.Popen(["gdb vuln"], stdin = subprocess.PIPE,  shell = True)

在我的脚本中启动 gdb.(vuln 是我要检查的二进制文件)

to start gdb within my script. (vuln is the binary I want to examine)

由于 gdb 的一个关键特性是暂停附加进程的执行并允许用户在接收到 SIGINT (STRG+C) 时检查寄存器和内存,因此我确实需要一些方法来将 SIGINT 信号传递给它.

Since a key feature of gdb is to pause the execution of the attached process and allow the user to inspect registers and memory on receiving SIGINT (STRG+C) I do need some way to pass a SIGINT signal to it.

没有

self.process.send_signal(signal.SIGINT)

也没有

os.kill(self.process.pid, signal.SIGINT)

os.killpg(self.process.pid, signal.SIGINT)

为我工作.

当我使用这些功能之一时,没有响应.我想这个问题是由使用 shell=True 引起的.但是,在这一点上,我真的没有想法.这次即使是我的老朋友 Google 也无法真正帮助我,所以也许你可以帮助我.提前致谢.

When I use one of these functions there is no response. I suppose this problem arises from the use of shell=True. However, at this point I am really out of ideas. Even my old friend Google couldn't really help me out this time, so maybe you can help me. Thank's in advance.

干杯,迈克


解决方案

这对我有用:

import signal
import subprocess

try:
    p = subprocess.Popen(...)
    p.wait()
except KeyboardInterrupt:
    p.send_signal(signal.SIGINT)
    p.wait()

相关文章