d33tah
d33tah

Reputation: 11589

How to automatically reattach to a process controlled by valgrind from within gdb?

I made a memory error that is quite difficult to debug, happening every once in a few command-line runs, each taking about two hours to complete. Because of that, I thought it could be a good idea to create logs like this:

while true; do 
  valgrind ./command 2>&1 | tee command
  grep -q Invalid && break
done

The problem is that my debug logs and stack traces produced by Valgrind aren't enough, so I decided to add --vgdb-error=0 to the command line. Unfortunately, since Valgrind now adds a breakpoint on startup, I need to run the following:

$ gdb ./command
...gdb init string follows...
(gdb) target remote | /usr/lib/valgrind/../../bin/vgdb
Remote debugging using | /usr/lib/valgrind/../../bin/vgdb
relaying data between gdb and process 4361
Reading symbols from /lib/ld-linux.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib/ld-linux.so.2
[Switching to Thread 4361]
0x04000840 in ?? () from /lib/ld-linux.so.2
(gdb) continue
Continuing.

How can I script the process so that either Valgrind does not break on startup or a script keeps attaching to vgdb processes and tell them to continue until one of the processes completes abnormally?

Upvotes: 2

Views: 486

Answers (1)

bonsaiviking
bonsaiviking

Reputation: 6005

The argument to --vgdb-error is the number of errors to expect before valgrind stops the program and attaches the debugger. If you set it to 0, then it will stop immediately before running your program. You want --vgdb-error=1, which will run to the first error then stop. Then you can attach a debugger.

Upvotes: 2

Related Questions