title: How do I attach gdbserver to a process at startup?
body: <p>I am using gdbserver to debug a remote process. I am able to attach gdbserver to the process after it has launched and is waiting for input.</p>
  
  <p>However, I want to attach gdbserver to the process while it is being launched. The process is launched via a shell script and I cannot change how this process is being launched i.e. I cannot modify the shell script to launch the process via a call to gdbserver.</p>
  
  <p>How do I attach gdbserver to this process as soon as it launches?</p>
  
  <p><strong>Edit:</strong> I am able to create a wait loop at the start of <code>main()</code>. For example a loop which waits till it finds a file at a predetermined location :</p>
  
  <pre><code>#include &lt;unistd.h&gt;
  int main() {
  
      while( access("/home/username/CONTINUE", F_OK) == -1)
          sleep(1);
  
      /*
              ...all the rest of main()
       */
  
      return 0;
  }
  </code></pre>
  
  <p>We can attach gdbserver while the process is busy with this loop, set breakpoints as required and say <code>touch /home/username/CONTINUE</code> to exit the loop. But this requires us to be able to access the source code, compile the binary and place it on the target machine. I am looking for a better, easier way than this.</p>