tags:

views:

217

answers:

3

I am currently running gdb version 6.7.1 on Ubuntu Linux, and working in a C++ project.

Surprisingly I was trying to debug a constructor and I have found that local variables declared within the scope of the constructor are not followed or noticed by gdb. Is this a bug ?

Thanks for any information ..

+2  A: 

If you are using optimization (-O), disable it (remove the -O or use -O0). The compiler is sometimes too smart and guesses a variable it not needed, do calculations at compile time, change a variable's scope, or several other tricks.

Note that even with "no optimization", some trivial optimizations are still done, but they shouldn't interfere much with debugging. Also, with inline-heavy use of C++ (including the STL), your program can become much slower without optimization.

CesarB
you answer is "general advice" and is not addressing the specific issue.
Trevor Boyd Smith
+2  A: 

It sounds like you are debugging an optimized build.

The debugger 'knows' the value of your local variables because the symbol file describes their location in the functions stack frame.

The debugger can then read the variables out of the memory of the target process. However, this requires that the stack frame contains up to date copies of the local variables. When compiling without optimizations the generated code will always write local variables back to their stack frame locations every time they are modified. This makes debugging easy, but costs at runtime.

For an optimized build the compiler will frequently deduce that these steps are unnecessary, and keep a value in a CPU register for as long as it is needed. It may well be that the local variable never gets a value written onto the stack at all. The debugger in this case has no way of tracking the value of the variable, but also doesn't know this and will often report data from the stack as if it were the variable value.

Rob Walker
+2  A: 

It is a bug in GCC, not in GDB.

It was recently fixed.