19

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

Gael Lorieul
  • 3,006
  • 4
  • 25
  • 50

3 Answers3

20

you can also use the comand

(gdb)monitor v.kill

it was listed on monitor help on gdb.

bucket
  • 216
  • 3
  • 4
  • At the `gdb` prompt (GNU gdb RHEL v7.11-67.el6), `monitor v.kill` resulted in: ``` "monitor" command not supported by this target. ``` – John Greene Jun 21 '17 at 18:45
  • @Egbert probably your version of gdb is different then mine (v 8.0). Try the comand "monitor help" to see if you gdb version has another command to kill the valgrind process. – bucket Jul 15 '17 at 01:34
2

Just use kill command or simply

k
  • to get rid of asking use set confirm off in .gdbinit file
Pazel1374
  • 218
  • 3
  • 14
0

The previous answers did not work for me, so I found this which did the trick.

  1. (gdb) info inferiors Should list all inferiors in gdb session, find the one with 'remote target' as its name, take note of the number on the left of it (will be 1 if no other inferiors running in gdb)
  2. (gdb) kill inferiors <number> Replace <number> with inferior number.
  3. (gdb) quit
Foffle
  • 1