Search code examples
pythongdbbacktracegdb-python

How to invoke "bt" command using JDebug for bt stack trace option


I am using Jdebug system call using python and trying to automate the process of "bt" command as shown below.

It consists of two steps.

jdebug core-tarball.0.tgz

Response received:

Using '/tmp' as temporary  location
jdebug version: 5.0.0
[File is compressed. This may take a moment...]
....
[Current thread is 1 (LWP 100276)]

(gdb) 

As seen above (gdb) prompt is appear and now i need to pass "bt" command and to read back the response from gdb prompt.

Not sure how to send "bt" command via python or at shell script and read the response back.

I am looking to automate these two steps:

Step #1: jdebug filename 
Step #2  bt

[invoke 'bt' on gdb prompt and read back the response lines i.e. stack_trace information] via pyton or via os.system call.


Solution

  • You can use Popen.communicate() for this.

    import subprocess
    p = subprocess.Popen(["jdebug", "core-tarball.0.tgz"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
    stdout_data, stderr_data = p.communicate("bt")
    print(stdout_data)
    print(p.returncode)