Quit valgrind is clean when debugging with gdb

I am debugging a program using valgrind and gdb. However, I am terminating these debugging sessions in a barbaric way ... Is this really how it should be done?

Setting up a debugging session

Following the instructions of the official valgrind website , I run the program:

  • I run valgrind by typing

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

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

     (gdb) target remote | vgdb 
  • I am running a program from the gdb CLI

     (gdb) c 

So far, so good: the program works in both terminals (one is used for valgrind and used for gdb). Then valgrind finds an error, such as an invalid read, and program execution is paused.

Session end

At this moment I want to play with my code: maybe fix something or comment / uncomment material from the source of the program. As a result, the program must be compiled again. A new binary file is created. After that, I want to stop the ongoing valgrind and gdb sessions (which used the old binary) and start new valgrind and gdb sessions that will use the new binary.

To stop the current session, I exit gdb

 (gdb) q 

Sometimes valgrind notices that gdb no longer exists and also exits. But in other cases, valgrind continues to grow, although the gdb process no longer exists ...

In this case, I kill the memcheck-amd64- process corresponding to my valgrind session. The number of this process is indicated in the valgrind messages, for example. 16195 in ==16195== Invalid read of size 8 ).

 kill -9 16195 

Regular killing is not enough: I need to use the -9 option.

I don’t think the call to kill -9 is how it should be done ... Am I missing something?

valgrind version: 3.10.1

gdb version: 7.7.1

+5
source share
2 answers

you can also use the command

 (gdb)monitor v.kill 

it was listed in the monitor help on gdb.

+4
source

The following works for me:

  • Disconnect from gdb first: (gdb) detach .
  • Then close gdb : (gdb) quit .
  • Then CTRL + C valgrind.
-2
source

All Articles