Search code examples
debugginggdbvalgrindterminate

Quit valgrind cleanly when debugging with gdb


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 ?

Setting up the debugging session

Following the instructions from the official valgrind website I do the following to run the program :

  1. I run valgrind by entering

    valgrind --tool=memcheck --vgdb=yes --vgdb-error=0 ./prgm.run 
    
  2. From another terminal session, I run gdb using

    gdb ./prgm.run
    
  3. I connect gdb to valgrind

    (gdb) target remote | vgdb
    
  4. I run the program from gdb CLI

    (gdb) c
    

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.

Terminating the session

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.

To stop an on-going session, I quit gdb

(gdb) q

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…

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. 16195 in ==16195== Invalid read of size 8).

kill -9 16195

A regular kill is not enough : I need to use the -9 option.

I don't think invoking kill -9 is how it is meant to be done… Am I missing something ?

valgrind version : 3.10.1

gdb version : 7.7.1


Solution

  • you can also use the comand

    (gdb)monitor v.kill
    

    it was listed on monitor help on gdb.