views:

331

answers:

4

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

+11  A: 

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.
a1kmm
The flaw with printf debugging is it can mask optimization bugs since the code to pass data into the printf call can prevent the code from performing the same optimization. (And I've had many instances where debug code works and optimized doesn't turn out to be optimizer bugs. OTOH, printf will help you find optimizer bugs and are a clue that you need to dive into the assembly to determine if you have an optimizer bug.
jmucchiello
+1 for suggesting debug builds and valgrind -- assuming that you haven't stumbled on a compiler bug, the only reason that an optimized build will fail when a debug build won't is that you're stomping on a different (and more important) part of memory
kdgregory
+1  A: 

It's always easier to debug a non-optimized version, of course. Failing that, disassembly of the code can be helpful. Other techinques I've used include partially de-optimizing the code by forcing intermediate results to be printed or logged, or changing a critical variable to "volatile" so I can at least look at that value in the debugger.

Mark Bessey
+4  A: 

When debugging release builds you can put in __asm nops; as a placeholder for breakpoints (int 3). This is nice as you can guarantee breakpoint locations without messing up compiler optimizations or writing printf/cout statements.

I don't know about GCC, but having an inline assembly block in MSVC can affect the optimization: "some other function-wide optimizations will be affected by the inclusion of assembly language in a function" - http://msdn.microsoft.com/en-us/library/5hd5ywk0.aspx
Michael Burr
A: 

Chances are what you call optimized code is scrambled to shave cycles (which makes debugging hard) but is not really very optimized. Here is an example of what I mean.

I would turn off the compiler optimization, debug and tune it yourself, and then turn compiler optimization back on if the code has hotspots that are actually in code the compiler sees (not in outside libraries). (I define a hotspot as a part of code where the PC is often found. That automatically exempts loops containing function calls because they steal away the PC.)

Mike Dunlavey