Local variables in constructors are not respected. Is this a bug in gdb?

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

Surprisingly, I tried to debug the constructor, and I found that local variables declared within the constructor are not respected or not noticed by gdb. This is mistake?

Thanks for any info ..

+4
source share
3 answers

This is a bug in GCC, not GDB.

Recently fixed .

+5
source

If you use optimization (-O), disable it (remove -O or use -O0). The compiler is sometimes too smart and guesses a variable in which it is not needed, performs calculations at compile time, changes the scope of variables or several other tricks.

Please note that even with β€œno optimization” some trivial optimizations are still performed, but they should not interfere much with debugging. Also, with heavy use of C ++ (including STL), your program can become much slower without optimization.

+2
source

It looks like you're debugging an optimized build.

The debugger "knows" the meaning of your local variables, since the symbol file describes their location in the frame of the function stack.

The debugger can then read the variables from the memory of the target process. However, this requires the stack frame to contain updated copies of local variables. When compiling without optimization, the generated code will always write local variables back to their location in the stack frames each time they are changed. This simplifies debugging, but costs at runtime.

For optimized builds, the compiler often infers that these steps are not needed, and keep the value in the CPU register for as long as necessary. It is possible that a local variable never gets the value written to the stack at all. The debugger in this case cannot track the value of the variable, but also does not know this and often reports data from the stack, as if it were the value of a variable.

+2
source

All Articles