捕获崩溃子进程的“段错误”消息:调用 communicate() 后没有输出和错误

20 投票
3 回答
17133 浏览
提问于 2025-04-17 21:17

我在使用subprocess模块时遇到了一些问题,想要获取崩溃程序的输出。

我正在用python2.7和subprocess来调用一个程序,这个程序有一些奇怪的参数,我希望能引发一些段错误(segfault)。

为了调用这个程序,我用以下代码:

proc = (subprocess.Popen(called,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE))
out,err=proc.communicate()
print out,err

这里的called是一个列表,里面包含了程序的名字和一个参数(这个参数是一个包含随机字节的字符串,除了NULL字节,因为subprocess对NULL字节不太友好)。

当程序正常运行时,这段代码可以正常显示标准输出和错误输出,但如果程序崩溃了,out和err就会变成空的,而不是显示著名的“段错误”。

我希望能找到一种方法,即使程序崩溃了,也能获取到out和err的内容。

我还尝试了check_output、call和check_call这些方法。

一些额外的信息:

  • 我在Archlinux 64位系统上运行这个脚本,并且是在一个python虚拟环境中(这可能不太重要,但谁知道呢 :p)

  • 段错误发生在我尝试运行的C程序中,是由于缓冲区溢出引起的。

  • 问题是,当段错误发生时,我无法通过subprocess获取到发生了什么的输出。

  • 我得到了正确的返回码:-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)。

3 个回答

-1
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 的原因。

12

当你看到 "Segmentation fault" 这个消息时,可能是由一个命令行程序产生的。要确认这个程序是不是因为 SIGSEGV 信号被杀掉的,可以检查 proc.returncode == -signal.SIGSEGV

如果你想看到这个消息,可以在命令行中运行以下命令:

#!/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 这个标准库模块:

#!/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
0

我回到这里:在python3中用subprocess这个模块运行得非常顺利。如果你在用linux系统,还有一个给python2用的版本,叫做subprocess32,也能很好地工作。

以前的解决办法:我用过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)
    

补充一下:这个方法稍微慢一点(因为它会生成一个伪终端)。

撰写回答