Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Valgrind debugger step through a program

Tags:

c++

valgrind

Good morning, I am trying to use the Valgrind debugger to step through a program.

My valgrind command line is valgrind -tool memcheck --leak-check=full --db-enable=yes ./MatchUpAcurate.exe.

I am using valgrind-3.5.0 on Centos Linux release 5.5 with gdb version 7,0.1-23.el5_5.2.

I enter Yes when valgrind asks the question Attach to Debugger. Then, the valgrind debugger returns with: 4428: return new tuple2<int,A>(2, i++, p->next());

When I try to use the gdb step or continue command, valgrind says

[New Thread 0x410fd10 (LWP 6548] Cannot find user-level thread for LWP 6551: generic error.

When I try to use valgrind --single-step=yes debugger option on the valgrind command-line, valgriind says Bad option aborting.

Could any valgrind users show me how to step through C++ source code or continue through a program? Thank you.

like image 975
Frank Avatar asked Feb 09 '11 13:02

Frank


People also ask

Can you run GDB with Valgrind?

Using Valgrind and GDB togetherStart up two terminal windows so that you can interact with Valgrind and GDB simultaneously. In one terminal, run Valgrind with the --vgdb-error=0 option. When running with --vgdb-error= n, Valgrind waits for n errors to occur before pausing and waiting for a connection from GDB.

How do you call Valgrind in C++?

Valgrind is readily usable for C/C++ code, but can even be used for other languages when configured properly (see this for Python). To run Valgrind, pass the executable as an argument (along with any parameters to the program). The flags are, in short: --leak-check=full : "each individual leak will be shown in detail"


2 Answers

You can also get vgdb in the 3.7.0 release. From the release notes:

  • GDB server: Valgrind now has an embedded GDB server. That means it is possible to control a Valgrind run from GDB, doing all the usual things that GDB can do (single stepping, breakpoints, examining data, etc). Tool-specific functionality is also available. For example, it is possible to query the definedness state of variables or memory from within GDB when running Memcheck; arbitrarily large memory watchpoints are supported, etc. To use the GDB server, start Valgrind with the flag --vgdb-error=0 and follow the on-screen instructions.

There's more info in the valgrind online manual.

like image 178
User123abc Avatar answered Oct 20 '22 19:10

User123abc


I asked the valgrind developers how to create a valgrind debugger. Here is what they said;

  1. Download the 3.6.0 source files from the valgrind website.

  2. Then you have to apply the patch which is in the bug 214909.

  3. Once properly compiled, you launch your application like this:

    valgrind --vgdb=yes --vgdb-error=0 ./prog
    

    and then in another window:

    gdb ./prog
    target remote | vgdb
    
  4. Do not start an external gdbserver : what the patch does is to integrate a gdbserver inside valgrind. This gdbserver integrated inside valgrind is activated by the --vgdb=yes.

like image 41
Frank Avatar answered Oct 20 '22 19:10

Frank