简体   繁体   中英

<Optimized out > in gdb in ubuntu

While debugging using gdb in ubuntu, after a break point I tried to print some thing. It showed <value optimized out > . In the make file I have changed -O2 to -O0 , even then the same error occurs. How to get rid of this error and print the optimized out value?

The dreaded "optimized out" message means that the compiler decided that it can't tell the debugger where the variable lies. It could be totally gone, or it could just be too complicated to represent in some way (perhaps in the interests of sanity inside the compiler code).

There are a few techniques you can use to avoid this message.

First, make sure -fvar-tracking is enabled. It should be the default when optimizing if you are using DWARF (side note: never use stabs), but it doesn't hurt to make sure. You may want -fvar-tracking-assignments as well. These flags tell GCC to try harder to generate debug info for variables.

If that fails, and you still need a particular variable, you can try compiling with less optimization. -O0 should work. I see in the post that you say it does not work, but in my long experience working on gdb, this always turns out to be some kind of developer error -- you forgot to rebuild the correct file, you are using the wrong library, something like that. You could also try -Og , which tries to enable only optimizations that won't hurt debugging.

One final option is to try to make the variable in question live at the point at which you're interested in it. For example you can take its address and pass it to some other function, being careful not to let the compiler optimize that function away.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM