title: How to automatically reattach to a process controlled by valgrind from within gdb?
body: <p>I made a memory error that is quite difficult to debug, happening every once in a few command-line runs, each taking about two hours to complete. Because of that, I thought it could be a good idea to create logs like this:</p>
  
  <pre><code>while true; do 
    valgrind ./command 2&gt;&amp;1 | tee command
    grep -q Invalid &amp;&amp; break
  done
  </code></pre>
  
  <p>The problem is that my debug logs and stack traces produced by Valgrind aren't enough, so I decided to add <code>--vgdb-error=0</code> to the command line. Unfortunately, since Valgrind now adds a breakpoint on startup, I need to run the following:</p>
  
  <pre><code>$ gdb ./command
  ...gdb init string follows...
  (gdb) target remote | /usr/lib/valgrind/../../bin/vgdb
  Remote debugging using | /usr/lib/valgrind/../../bin/vgdb
  relaying data between gdb and process 4361
  Reading symbols from /lib/ld-linux.so.2...(no debugging symbols found)...done.
  Loaded symbols for /lib/ld-linux.so.2
  [Switching to Thread 4361]
  0x04000840 in ?? () from /lib/ld-linux.so.2
  (gdb) continue
  Continuing.
  </code></pre>
  
  <p>How can I script the process so that either Valgrind does not break on startup or a script keeps attaching to vgdb processes and tell them to continue until one of the processes completes abnormally?</p>