gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 19:52:23 24 4
gpt4 key购买 nike

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

到目前为止我使用

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

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

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

都不是

self.process.send_signal(signal.SIGINT)

也不

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

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

为我工作。

当我使用其中一个功能时没有任何响应。我认为这个问题是由于使用 shell=True 引起的。然而,此时我真的没有想法了。就连我的老 friend 谷歌这次也帮不了我,所以也许你可以帮助我。提前致谢。

干杯,迈克

最佳答案

这是对我有用的:

import signal
import subprocess

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

关于python - 如何使用 shell = true 通过 Python subprocess.Popen() 将 SIGINT 传递给子进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59752715/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com