GCC gOlogy: studying the impact of optimizations on debugging

ingve | 61 points

More times than I'd like, I've encountered GDB refusing to show the value of a variable, claiming it's "optimized away".

No, it's sitting right there in a register, and I know that because I just stepped through, instruction-by-instruction, the code that computed its value. Fortunately it does not claim the register has been "optimized away" too, but the experience of debugging optimised code could definitely use a lot of improvement.

(This isn't exclusive to GCC/GDB either --- I've seen the same with MSVC and Visual Studio, but in my experience the latter tends to be somewhat better.)

userbinator | 6 years ago

Thanks for sharing. I wish there was a similar article regarding the different quirks of Link-Time Optimization and its effect on debugging, and how that impact has changed since GCC 4.x. Also, an explanation of how LTO's link-time optimization flags are chosen, and what happens when they're absent. And most importantly, why -O0 has an impact on LTO when it's passed at link-time, but eg. -O2/-O1 at compile time.

Supersaiyan_IV | 6 years ago

What I'd appreciate is if GDB, when stopped during execution, could show _all_ of the source statements that are currently 'in progress' (for the common case under optimisation where the execution of several source statements are interleaved). I don't know how feasible that is, though.

caf | 6 years ago

I kept reading and reading wondering, what is the point of this document? I make it 1/3rd through.

It seems well informed, but it lacks any coherent structure. And throughout I couldn't deduce what the point was. Like a well educated rant.

cjhanks | 6 years ago

If you approach debugging in a very minimalist way,

you should never use a debugger

because it only serves to complicate things

using the I/O subsystem to debug your program

is a far more efficient way to debug.

If you do things this way,

you orthogonalize the optimization side of your compiler

from the mess created by trying to assist the debugging process

via the rather complex and intrusive (from the compiler's perspective) component called the debugger

khitchdee | 6 years ago