20

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)
    
  • シェルの戻り値は同じです: echo $? 139 を返すので -11 ($? & 128)

4

3 に答える 3

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)'"segfault を引き起こすことでそれをテストしました、そして、メッセージは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

またはptystdlib モジュールを直接使用する:

#!/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
于 2014-03-07T14:57:47.533 に答える
0

subprocess32ここに戻ってきました:それはpython3のサブプロセスで魅力のように機能し、Linuxを使用している場合は、非常にうまく機能する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 を生成するため)

于 2014-03-11T19:46:18.443 に答える
-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()可能であれば使用してください。stderrstderr=STDOUT

于 2014-03-07T13:16:41.923 に答える