How do I view the value of an <optimized out> variable in C++?
Create your own 'global variable' and print the optimized out variable into this global variable. Make sure to remove these globals created by you after you are done with the debugging!
If you can't or don't want to disable optimization, then you can try declaring the variable as volatile. This is usually enough to make your compiler preserve the variable in the final code.
Alternatively, in recent GCC versions you can disable optimization for just a function, like this:
void my_function() __attribute__((optimize(0)))
{
int x = floor(sqrt(3));
}
When using reverse debugging, try to step back closer to the definition point of the variable
As shown at: What does <value optimized out> mean in gdb? it is often the case that within functions:
- at the start of the function, the value of variables can be observed
- towards the end of the function however, the variable is more and more likely to become
<optimized out>
, as it was stored only in a register due to optimizations, and not on memory on the stack. So when it is not needed anymore, the register is likely to be reused and overwritten by another variable, and then the debug metadata informs GDB of that.
Therefore, if you are using some kind of reverse debugging such as Mozilla rr
, which you will do all the time once you try it once, then one good bet is to try and step back closer to the point of definition/last usage of the variable with reverse-finish
+ reverse-next
and see if you can observe it there.
This can be observed concretely with the example code shown at What does <value optimized out> mean in gdb? and has saved me a few times, especially when running the unoptimized program makes it take too long to reach the point of interest (which is unsurprising given the terribly inefficient assembly generated by -O0
as seen on that answer).
On high optimization levels, the compiler can eliminate intermediate values, as you have seen here. There are a number of options:
- You can reduce the optimization level to make it easier for the debugger to keep track of things.
-O0
is certain to work (but will be quite a lot slower),-O1
might work okay as well. - You can add some explicit print statements to log the output value.
- You can also usually force the compiler to retain this specific value by making it volatile (but remember to un-make it volatile when you're done!). Note, however, that since control flow is also subject to alteration in optimized code, even if you can see the value of the variable, it may not be entirely clear what point in the code you're at when you're looking at the variable in question.