問題描述
我在使用 subprocess 模塊獲取崩潰程序的輸出時遇到問題.我正在使用 python2.7 和 subprocess 調用帶有奇怪參數的程序以獲得一些段錯誤為了調用程序,我使用以下代碼:
I have problems using the subprocess module to obtain the output of crashed programs. I'm using python2.7 and subprocess to call a program with strange arguments in order to get some segfaults In order to call the program, I use the following code:
proc = (subprocess.Popen(called,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE))
out,err=proc.communicate()
print out,err
被調用的是一個包含程序名稱和參數的列表(一個包含隨機字節的字符串,除了子進程根本不喜歡的 NULL 字節)
called is a list containing the name of the program and the argument (a string containing random bytes except the NULL byte which subprocess doesn't like at all)
當程序沒有崩潰時,代碼會運行并向我顯示 stdout 和 stderr,但是當它確實崩潰時,out 和 err 為空,而不是顯示著名的分段錯誤".
The code behave and show me the stdout and stderr when the program doesn't crash, but when it does crash, out and err are empty instead of showing the famous "Segmentation fault".
我希望找到一種方法,即使程序崩潰也會出錯.
I wish to find a way to obtain out and err even when the program crash.
我也試過 check_output/call/check_call 方法
I also tried the check_output / call / check_call methods
一些附加信息:
我在 python 虛擬環境中的 Archlinux 64 位上運行這個腳本(這里不應該是重要的東西,但你永遠不知道:p)
I'm running this script on an Archlinux 64 bits in a python virtual environment (shouldn't be something important here, but you never know :p)
段錯誤發生在我嘗試運行的 C 程序中,是緩沖區溢出的結果
The segfault happens in the C program I'm trying to run and is a consequence of a buffer overflow
問題是當segfault發生時,我無法得到子進程發生了什么的輸出
The problem is that when the segfault occurs, I can't get the output of what happened with subprocess
我得到正確的返回碼:-11 (SIGSEGV)
I get the returncode right: -11 (SIGSEGV)
使用python我得到:
Using python i get:
./dumb2 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
('Exit code was:', -11)
('Output was:', '')
('Errors were:', '')
在 python 之外我得到:
While outside python I get:
./dumb2 $(perl -e "print 'A'x50")
BEGINNING OF PROGRAM
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
END OF THE PROGRAM
Segmentation fault (core dumped)
shell的返回值是一樣的:echo $?返回 139 所以 -11 ($? & 128)
The return value of the shell is the same: echo $? returns 139 so -11 ($? & 128)
推薦答案
回到這里:它就像 python3 的 subprocess 的魅力,如果你在 linux 上,有一個名為 subprocess32
的向后移植到 python2代碼> 效果很好
Came back here: it works like a charm with subprocess from python3 and if you are on linux, there is a backport to python2 called subprocess32
which does work quite well
較舊的解決方案:我使用了 pexpect,它可以工作
Older solution: I used pexpect and it works
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)
PS: a little slower (because it spawns a pseudo tty)
這篇關于捕獲“分段錯誤"崩潰子進程的消息:調用communication() 后沒有輸出和錯誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!