Search code examples
cgdbvalgrind

Connecting valgrind to GDB


The official valgrind documentation(https://valgrind.org/docs/manual/manual-core-adv.html#manual-core-adv.gdbserver) provides us with the following instructions:

" 3.2. Debugging your program using Valgrind gdbserver and GDB ... 3.2.1. Quick Start: debugging in 3 steps The simplest way to get started is to run Valgrind with the flag --vgdb-error=0. Then follow the on-screen directions, which give you the precise commands needed to start GDB and connect it to your program. "

I execute valgrind --vgdb-error=0 ./badcode and get

==101901== TO DEBUG THIS PROCESS USING GDB: start GDB like this
==101901==   /path/to/gdb ./badcode
==101901== and then give GDB the following command
==101901==   target remote | /usr/lib/x86_64-linux-gnu/valgrind/../../bin/vgdb --pid=101901

After that I open GDB gdb badcode, type in start and copy the command valgrind gave me:

target remote | /usr/lib/x86_64-linux-gnu/valgrind/../../bin/vgdb --pid=101901

The output is:

A program is being debugged already.  Kill it? (y or n)

If I chose y it follows with

Remote debugging using | /usr/lib/x86_64-linux-gnu/valgrind/../../bin/vgdb --pid
=101901
sh: 1: /usr/lib/x86_64-linux-gnu/valgrind/../../bin/vgdb: not found
Remote communication error.  Target disconnected.: Connection terminated by the other party.

And if I chose n I get

`Program not killed.
(gdb)`

How can I solve this problem?


Solution

  • As Gehardh said, the problem is, that the path valgrind tells you is wrong. It tells you it is at /usr/lib/x86_64-linux-gnu/valgrind/../../bin/vgdb, which is equivalent to /usr/lib/bin/vgdb and not /usr/bin/vgdb where it should be. Using /usr/bin/vgdb should fix that problem. A other minor point is, you don't need and better shouldn't start the program in gdb with start. In other words, start gdb and type: target remote | /usr/bin/vgdb, without typing start first. (In case it is the only valgrind process, the pid is not needed, otherwise add the --pid=<pid> to the command).

    I found this bugreport where valgrind prints the wrong path, but it is older and should be fixed. Maybe you should update your system (in case it is the same bug)?