title: Quit valgrind cleanly when debugging with gdb
body: <p>I am debugging a program using valgrind and gdb. However I terminate those debugging sessions in a barbaric way… Is it really how it is meant to be done ?</p>
  
  <h1>Setting up the debugging session</h1>
  
  <p>Following the instructions from the <a href="http://valgrind.org/docs/manual/manual-core-adv.html#manual-core-adv.gdbserver-gdb" rel="noreferrer">official valgrind website</a> I do the following to run the program :</p>
  
  <ol>
  <li><p>I run valgrind by entering</p>
  
  <pre><code>valgrind --tool=memcheck --vgdb=yes --vgdb-error=0 ./prgm.run 
  </code></pre></li>
  <li><p>From another terminal session, I run gdb using</p>
  
  <pre><code>gdb ./prgm.run
  </code></pre></li>
  <li><p>I connect gdb to valgrind</p>
  
  <pre><code>(gdb) target remote | vgdb
  </code></pre></li>
  <li><p>I run the program from gdb CLI</p>
  
  <pre><code>(gdb) c
  </code></pre></li>
  </ol>
  
  <p>So far so good : the program runs in both terminals (the one used for valgrind and the one used for gdb). Then valgrind finds an error, for instance an invalid read, and the program execution is paused.</p>
  
  <h1>Terminating the session</h1>
  
  <p>At that point, I want to fiddle with my code : perhaps fix something or comment/uncomment stuff from the program's source. As a consequence, the program needs to be compiled anew. A new binary is generated. Following that, I want to stop the on-going valgrind and gdb sessions (that were using the old binary) and start new valgrind and gdb sessions that will use the new binary.</p>
  
  <p>To stop an on-going session, I quit gdb</p>
  
  <pre><code>(gdb) q
  </code></pre>
  
  <p>Sometimes valgrind notices that gdb is no longer there and quits too. But other times valgrind keeps going even-though no gdb process exist anymore…</p>
  
  <p>In that case I kill the "memcheck-amd64-" process corresponding to my valgrind session. The number of that process is indicated in the valgrind messages e.g. <code>16195</code> in <code>==16195== Invalid read of size 8</code>).</p>
  
  <pre><code>kill -9 16195
  </code></pre>
  
  <p>A regular kill is not enough : I need to use the <code>-9</code> option.</p>
  
  <p>I don't think invoking <code>kill -9</code> is how it is meant to be done… Am I missing something ?</p>
  
  <p>valgrind version : 3.10.1</p>
  
  <p>gdb version : 7.7.1</p>