我在使用 subprocess 模块获取崩溃程序的输出时遇到问题。 我正在使用 python2.7 和子进程来调用带有奇怪参数的程序以获得一些段错误 为了调用该程序,我使用以下代码:
proc = (subprocess.Popen(called,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE))
out,err=proc.communicate()
print out,err
称为是一个包含程序名称和参数的列表(一个包含随机字节的字符串,除了子进程根本不喜欢的NULL字节)
当程序没有崩溃时,代码会显示 stdout 和 stderr,但当程序崩溃时,out 和 err 为空,而不是显示著名的“分段错误”。
我希望找到一种方法,即使程序崩溃也能摆脱错误。
我还尝试了 check_output / call / check_call 方法
一些附加信息:
我在Python虚拟环境中的Archlinux 64位上运行这个脚本(这里不应该是重要的事情,但你永远不知道:p)
段错误发生在我尝试运行的 C 程序中,是缓冲区溢出的结果
问题是,当发生段错误时,我无法获取子进程发生的情况的输出
我得到了正确的返回码:-11 (SIGSEGV)
使用 python 我得到:
./dumb2 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
('Exit code was:', -11)
('Output was:', '')
('Errors were:', '')
在 python 之外我得到:
./dumb2 $(perl -e "print 'A'x50")
BEGINNING OF PROGRAM
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
END OF THE PROGRAM
Segmentation fault (core dumped)
shell的返回值是一样的:echo $?返回 139 所以 -11 ($? & 128)
"Segmentation fault"
消息可能由 shell 生成。要了解进程是否被 SIGSEGV
杀死,请检查 proc.returncode == -signal.SIGSEGV
。
如果你想查看该消息,可以在 shell 中运行以下命令:
#!/usr/bin/env python
from subprocess import Popen, PIPE
proc = Popen(shell_command, shell=True, stdout=PIPE, stderr=PIPE)
out, err = proc.communicate()
print out, err, proc.returncode
我用
shell_command="python -c 'from ctypes import *; memset(0,1,1)'"
测试了它,这会导致段错误,并且消息被捕获在 err
中。
如果消息直接打印到终端,那么您可以使用
pexpect
模块来捕获它:
#!/usr/bin/env python
from pipes import quote
from pexpect import run # $ pip install pexpect
out, returncode = run("sh -c " + quote(shell_command), withexitstatus=1)
signal = returncode - 128 # 128+n
print out, signal
或者直接使用
pty
stdlib 模块:
#!/usr/bin/env python
import os
import pty
from select import select
from subprocess import Popen, STDOUT
# use pseudo-tty to capture output printed directly to the terminal
master_fd, slave_fd = pty.openpty()
p = Popen(shell_command, shell=True, stdin=slave_fd, stdout=slave_fd,
stderr=STDOUT, close_fds=True)
buf = []
while True:
if select([master_fd], [], [], 0.04)[0]: # has something to read
data = os.read(master_fd, 1 << 20)
if data:
buf.append(data)
else: # EOF
break
elif p.poll() is not None: # process is done
assert not select([master_fd], [], [], 0)[0] # nothing to read
break
os.close(slave_fd)
os.close(master_fd)
print "".join(buf), p.returncode-128
回到这里:它的工作方式就像一个来自 python3 的子进程的魅力,如果你在 Linux 上,有一个名为
subprocess32
的向后移植到 python2,它确实工作得很好
旧的解决方案:我使用了 pexpect 并且它有效
def cmd_line_call(name, args):
child = pexpect.spawn(name, args)
# Wait for the end of the output
child.expect(pexpect.EOF)
out = child.before # we get all the data before the EOF (stderr and stdout)
child.close() # that will set the return code for us
# signalstatus and existstatus read as the same (for my purpose only)
if child.exitstatus is None:
returncode = child.signalstatus
else:
returncode = child.exitstatus
return (out, returncode)
PS:有点慢(因为它产生了一个伪tty)
proc = (subprocess.Popen(called, stdout=subprocess.PIPE, stderr=subprocess.PIPE))
print(proc.stdout.read())
print(proc.stderr.read())
这应该效果更好。
就我个人而言,我会选择:
from subprocess import Popen, PIPE
handle = Popen(called, shell=True, stdout=PIPE, stderr=PIPE)
output = ''
error = ''
while handle.poll() is None:
output += handle.stdout.readline() + '\n'
error += handle.stderr.readline() + '\n'
handle.stdout.close()
handle.stderr.close()
print('Exit code was:', handle.poll())
print('Output was:', output)
print('Errors were:', error)
如果可能的话,可能会使用
epoll()
作为stderr
,因为它有时会阻止呼叫,因为它是空的,这就是为什么我在懒惰时最终会做stderr=STDOUT
。