捕获“分段故障"消息,并向其发送消息.崩溃子进程的消息:调用communication()后没有out和err [英] Capture "Segmentation fault" message for a crashed subprocess: no out and err after a call to communicate()

查看:146
本文介绍了捕获“分段故障"消息,并向其发送消息.崩溃子进程的消息:调用communication()后没有out和err的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用子进程模块获取崩溃程序的输出时遇到问题. 我正在使用python2.7和子进程来调用带有奇怪参数的程序,以获得一些段错误 为了调用该程序,我使用以下代码:

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.

希望有人在这里提供一个想法:)

Hope someone here as an idea :)

PS:我还尝试了check_output/call/check_call方法

PS: 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)

segfault发生在我尝试运行的C程序中,这是缓冲区溢出的结果

The segfault happens in the C program I'm trying to run and is a consequence of a buffer overflow

问题是发生段错误时,我无法获得子进程发生的结果

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 so -11($?& 128)

  • The return value of the shell is the same: echo $? returns 139 so -11 ($? & 128)

    推荐答案

    回到这里:它与python3的子进程一样具有魅力,如果您在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 the work quite well

    • 已解决:我使用了pexpect,它可以正常工作

    • SOLVED: I used pexpect and it works

    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)

    PS: a little slower (because it spawn a pseudo tty)

    这篇关于捕获“分段故障"消息,并向其发送消息.崩溃子进程的消息:调用communication()后没有out和err的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆