简体   繁体   English

如何使用Python创建GDB子进程并根据输出动态发送命令?

[英]How to use Python to create GDB subprocess and send commands dynamically based on output?

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. 例如,我将通过从程序向GDB发送命令“ bt”来启动回溯。 Then I would search for a word such as "pardrivr" and get the line number associated with it by using regular expressions. 然后,我将搜索诸如“ pardrivr”之类的单词,并使用正则表达式获取与其关联的行号。 Then I would input "f [line_number_of_pardriver]" into GDB. 然后我将“ f [line_number_of_pardriver]”输入到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. 我想使用Python通过创建GDB的子进程来完成此任务。 Then I want to send commands to GDB and read output from GDB. 然后,我想向GDB发送命令并从GDB读取输出。 I would use the output to send more commands to GDB dynamically based on the content of the output. 我将使用输出根据输出内容将更多命令动态发送到GDB。

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 使用Pexpect: http : //pexpect.readthedocs.org/en/latest/examples.html

A more basic option is using subprocess.Popen().communicate() : 一个更基本的选项是使用subprocess.Popen().communicate()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM