如何使用Python创建GDB子流程并根据输出动态发送命令? [英] How to use Python to create GDB subprocess and send commands dynamically based on output?

查看:129
本文介绍了如何使用Python创建GDB子流程并根据输出动态发送命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我需要做的一个通用示例:例如,我会通过从程序向GDB发送命令bt来启动回溯。然后,我将搜索pardrivr之类的单词,并使用正则表达式获取与其关联的行号。然后我会输入f [line_number_of_pardriver]到GDB中。这个过程将被重复,直到最终提取正确的信息为止。



我想通过创建GDB的子进程来使用Python来实现这一点。然后我想发送命令到GDB并从GDB读取输出。我会使用输出来根据输出的内容动态地发送更多的命令给GDB。



有人可以提供一个简单的例子来说明如何做到这一点吗?



非常感谢您!

解决方案

使用Pexpect: http://pexpect.readthedocs.org/en/latest/examples.html



更基本的选择是使用 subprocess.Popen()。communicate()

http://pymotw.com/2/subprocess/#popen2


Here's a general example of what I need to do:

For example, I would initiate a back trace by sending the command "bt" to GDB from the program. Then I would search for a word such as "pardrivr" and get the line number associated with it by using regular expressions. Then I would input "f [line_number_of_pardriver]" into GDB. This process would be repeated until the correct information is eventually extracted.

I want to use Python to accomplish this by creating a subprocess of GDB. Then I want to send commands to GDB and read output from GDB. I would use the output to send more commands to GDB dynamically based on the content of the output.

Could someone please provide a simple example of how to do this?

Thank you so much!

解决方案

Use Pexpect: http://pexpect.readthedocs.org/en/latest/examples.html

A more basic option is using subprocess.Popen().communicate():

http://pymotw.com/2/subprocess/#popen2

这篇关于如何使用Python创建GDB子流程并根据输出动态发送命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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