How to use Python to create GDB subprocess and sen

2019-08-16 21:19发布

问题:

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!

回答1:

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