tags:

views:

890

answers:

4

I have an application that only crashes in -O2 optimization (compiled with gcc 4.2.4). When I step through the code and get to the spot that crashes and try to inspect the value, I get a "value optimized out" in gdb.

I read on the internet that this means that the value is stored in the register. I was wondering if my crash could be related to the fact that some information is placed in registers? Is there a way to print what is in the registers to see if it has been corrupted? Is there a way to keep optimizations but not use registers?

Thanks!

+3  A: 

try info registers in gdb.

You can disable optimization with -O0, but there's something fishy and I suggest you to investigate further and eventually post the code.

Stefano Borini
+7  A: 

It's 99% likely to be a bug in your code and 1% likely to be a compiler code generation bug. So spend a proportionate amount of time looking for latent bugs in your code but be aware that you just may have found a code generation bug (in which case you'll need to study the compiler generated code carefully to see what the problem is).

Paul R
More like 99.9% and 0.1%. Optimizers have the amazing ability to break subtly buggy code even when it normally runs fine.
caspin
@Caspin - yes, you're probably right - even 1 bug in 1000 being due to the compiler is perhaps overstating it. I think I've only ever seen a handful of genuine compiler bugs (in mature, stable compilers, at least) in the past 25 years.
Paul R
+2  A: 

If you can detect the error in your program flow you could do some printing yourself, if it has something to do with memory leaks and memory corruption, then valgrind is probably a better friend than gdb.

amo-ej1
A: 

This is not a problem, it is more of an issue with the aggressive optimizations in newer versions of gcc.

See: A Plan to Fix Local Variable Debug Information in GCC.

Adam Goode
How can you be so sure? We have not seen his code; of course this may be a bug in gcc's optimizations, but until we get more information...
Arthur Reutenauer