web-dev-qa-db-ja.com

クラッシュしたサブプロセスの「セグメンテーション違反」メッセージをキャプチャします。communicate()の呼び出し後にアウトとエラーが発生しません

クラッシュしたプログラムの出力を取得するためにサブプロセスモジュールを使用する際に問題が発生しました。私はpython2.7とサブプロセスを使用して、いくつかのセグメンテーション違反を取得するために奇妙な引数でプログラムを呼び出しています。プログラムを呼び出すために、次のコードを使用します。

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

呼び出されるのは、プログラムの名前と引数(サブプロセスがまったく気に入らないNULLバイトを除くランダムなバイトを含む文字列)を含むリストです。

プログラムがクラッシュしない場合、コードは動作し、stdoutとstderrを表示しますが、クラッシュすると、有名な「セグメンテーション違反」を表示する代わりに、outとerrが空になります。

プログラムがクラッシュした場合でも、取得してエラーを発生させる方法を見つけたいと思います。

アイデアとしてここに誰かを願っています:)

PS:check_output/call/check_callメソッドも試しました

編集:

  • 私はこのスクリプトをpython仮想環境のArchlinux64ビットで実行しています(ここでは重要なことではないはずですが、: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)

18
Tic

編集:ここに戻ってきました:それはpython3からのサブプロセスで魅力のように機能します、そしてあなたがLinuxを使っているなら、subprocess32と呼ばれるpython2へのバックポートがありますそれは非常にうまく機能します

  • 解決済み:pexpectを使用しましたが、機能します

    def cmdlineCall(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を生成するため)

0
Tic

_"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 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
_
9
jfs