This post is from a tip that Michael Ballantyne passed along to me.

Occasionally we encounter tests that fail infrequently. Debugging these can be very frustrating. Here is one technique that can help.

Debug build

Add

-DCMAKE_CXX_FLAGS="-g -O3"

to your cmake configure line to include debug symbols but still do optimization. Inlining means that backtraces may not show exactly where errors occur, but it’s close enough and more likely to correctly reproduce than a build without optimization.

Running gdb

Per this StackOverflow answer, put the following in a file (here named commandfile):

set $_exitcode = -999
run
if $_exitcode != -999
quit
end

Then loop until gdb stalls on an error. In bash:

while gdb ./heateqn -x commandfile; do :; done

Finally, once gdb breaks on the segfault give it the command
backtrace to see details of the error.

Testing

If you’d like to make sure the script works, make a program that
always segfaults; put the following in segfault.c:

main;

Compile and run as:

while gdb ./a.out -x commandfile; do :; done

It should immediately halt in gdb. Type backtrace to see the trace,
or quit 1 to exit gdb with a non-zero exit code so that the while
loop terminates.

Categories: tips

0 Comments

Leave a Reply