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

1
  • Still an issue using valgrind v3.11.0 and gdb RHEL 7.11-67.el6. Jun 21, 2017 at 18:48

3 Answers 3

20

you can also use the comand

(gdb)monitor v.kill

it was listed on monitor help on gdb.

2
  • At the gdb prompt (GNU gdb RHEL v7.11-67.el6), monitor v.kill resulted in: ``` "monitor" command not supported by this target. ``` Jun 21, 2017 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, 2017 at 1:34
2

Just use kill command or simply

k
  • to get rid of asking use set confirm off in .gdbinit file
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.