title: How to attach gdb to a subprocess started from Python script?
body: <p>I have the following Python 3 script</p>
  
  <p><code>foobar.py</code>:</p>
  
  <pre><code>#!/usr/bin/python3
  
  import subprocess
  import sys
  
  p = subprocess.Popen(["./foobar"], stdin=subprocess.PIPE, shell=False)
  sys.stdin.read(1)
  p.stdin.write("f".encode())
  p.stdin.flush()
  sys.stdin.read(1)
  </code></pre>
  
  <p>which starts the program <code>foobar</code> compiled from the following file <code>foobar.c</code> with <code>-g</code> option:</p>
  
  <p><code>foobar.c</code>:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main() {
      char c;
  
      if (EOF == scanf("%c", &amp;c)) {
          perror(NULL);
          exit(1);
      }
  
      printf("received char %c\n", c);
      return(0);
  }
  </code></pre>
  
  <p>I start the script, it waits for me to enter a character, I hit <code>Enter</code>, and I get <code>f</code>:</p>
  
  <pre><code>&gt;./foobar.py
  
  received char f
  </code></pre>
  
  <p>OK, what I would like, is to inspect <code>foobar</code> with the debugger <code>gdb</code>.  That is what <code>sys.stdin.read(1)</code> is for:  I hoped to start</p>
  
  <pre><code>&gt;./foobar.py
  </code></pre>
  
  <p>and then, in another terminal, find out the process id of <code>foobar</code>, and run</p>
  
  <p><code>&gt;gdb attach</code>pid<code>-ex='b foobar.c:12'</code></p>
  
  <p>then I was hoping to hit <code>Enter</code> in the first terminal as before, and then the C program would eat the input and stop at the line 12, which is <code>printf</code>, as requested.  </p>
  
  <p>But it does not work this way - I hit <code>Enter</code> and nothing happens, the program <code>foobar</code> does not budge, it still waits at <code>scanf</code>. </p>
  
  <p>How to do it so that I can stop at <code>printf</code> ? </p>