3

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.

Ammad
  • 4,031
  • 12
  • 39
  • 62

2 Answers2

2

You can write to stdin using Popen of subprocess module:

from subprocess import Popen, PIPE
proc = Popen(['jdebug', 'core-tarball.0.tgz'], stdin=PIPE)
proc.stdin.write("bt\r")
  • I need to read the response back from here too after issuing bt command. How to do that as stdin is already engaged. – Ammad Aug 10 '20 at 07:38
1

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)
alexisdevarennes
  • 5,437
  • 4
  • 24
  • 38
  • I used this to print nice out put. print(stdout_data.decode('utf8', errors='strict').strip()) – Ammad Aug 13 '20 at 02:11