What are efficient ways to debug an optimized C/C++ program?

c, c++, debugging, disassembly

Solution

It is always harder to debug optimised programs, but there are always ways. Some additional tips:

- Make a debug build, and see if you get the same bug in a debug build. No point debugging an optimised version if you don't have to.

- Use valgrind if on a platform that supports it. The errors you see may be harder to understand, but catching the problem early often simplifies debugging.

- printf debugging is primitive, but sometimes it is the simplest way if you have a complex issue that only shows up in optimised builds.

- If you suspect a timing issue (especially in a multithreaded program), roll your own version of assert which aborts or prints if the condition is violated, and use it in a few select places, to rule out possible problems.

- See if you can reproduce the problem without using -fomit-frame-pointers, since that makes code very hard to debug, and with -O2 or -O3 enabled. That might give you enough information to find the cause of your problem.

- Isolate parts of your code, build a test-suite, and see if you can identify any testcases which fail. It is much easier to debug one function than the whole program.

- Try turning off optimisations one by one with the -fno-X options. This might help you find common problems like strict aliasing problems.

- Turn on more compiler warnings. Some things, like strict aliasing problems, can generate compiler warnings if they create a difference in behaviour between different optimisation levels.

Problem

Many times I work with optimized code (sometimes even involving vectorized loops), which contain bugs and such. How would one debug such code? I'm looking for any kind of tools or techniques. I use the following (possibly outdated) tools, so I'm looking to upgrade. I use the following: - Since with ddd, you cannot see the code, I use gdb+ dissambler command and see the produced code; I can't really step through the program using this. - ndisasm Thanks

Original source

Related problems