因此,我正在编写(或至少尝试)一个程序来比较python中两个gdb运行的输出。这就是我到目前为止所知道的:
from subprocess import *
import subprocess
file = raw_input('enter program name (with a ./ if in local directory): ')
p1 = Popen(['gdb', file], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p2 = Popen(['gdb', file], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p1.communicate(input='break main')
p2.communicate(input='break main')
args1 = raw_input('args for running program (from file) (ie r < input.txt): ')
args2 = raw_input('args for running program (from file) (for program 2...): ')
p1.communicate(input=args1)
p2.communicate(input=args2)
while True:
p1out = p1.communicate(input='continue')[0]
p2out = p2.communicate(input='continue')[0]
if p1out != p2out:
print 'difference: '
print 'p1: ' + p1out
print 'p2: ' + p2out
cont = raw_input('continue (y/n): ')
if cont != 'y':
break
现在的问题是,这似乎不起作用。你知道我可能会错在哪里吗?
更多细节:程序的重点是接收可执行文件,在main函数处中断,然后遍历每个函数,直到输出在两个函数之间发生变化。这是一种调试工具(我会使用它,即使其他人不会使用!)。然后,当您发现一个差异时,它会让您选择是结束程序,还是继续。从理论上讲,这应该是可行的,但我只是不确定哪里出了问题。
发布于 2013-06-26 01:14:00
.communicate
等待Popen
对象完成执行。由于您试图在gdb运行时与其对话,因此它将永远挂起。如果没有任何输入,gdb是不会退出的。此外,您需要自己编写换行符,以模拟用户按enter键。
你要做的就是在gdb执行的时候对其进行读写。为此,在发送输入时使用p1.stdin.write('break main\n')
(请注意'\n'
),在读取输出时使用p1.stdout.readline()
。这适用于开头的中断,您正在发送的args,以及继续。
在发送参数和请求执行时,还应该确保对gdb执行start
命令。
p1.stdin.write('start ' + args1 + '\n')
p2.stdin.write('start ' + args2 + '\n')
您还希望处理一个进程在另一个进程之前终止的情况。您可以使用Popen.poll
来检查进程是否已经完成,如果还没有完成,它将返回None
。尽管这可能不是您想要的处理方式,但您可以将循环的顶部更改为如下所示:
while True:
if p1.poll() is not None and p2.poll() is not None:
print 'p1 and p2 have both finished'
break
elif p1.poll() is not None:
print 'p1 finished before p2'
break
elif p2.poll() is not None:
print 'p2 finished before p1'
break
p1.stdin.write('continue\n')
p2.stdin.write('continue\n')
p1out = p1.stdout.readline()
p2out = p2.stdout.readline()
读取一行可能不正确,您必须校准读取的行数才能获得正确的输出。
您应该将读取内容添加到stderr
,或者如果您不关心它,则将其发送到/dev/null
。如果您不执行这两项操作,管道缓冲区可能会被填满并导致挂起。
https://stackoverflow.com/questions/17303260
复制相似问题