how to examine data with compiler optimization option set?

jreiver@free.fr jreiver@free.fr
Tue Sep 2 21:45:00 GMT 2008


Yes, I knew that the optimization option removes the visibility for a, b and c.
But GDB user manual seemed to say that you can still view them with providing
-gstabs or -gdwarf2 option, in addition to the -O[1|2|3] option you might want
to keep.

Isn't that possible at all? (I am currently evaluating the debugging facilities
of gdb)

Regards,

J R

Quoting Robert Dewar <dewar@adacore.com>:

> J R wrote:
> > Extract of GDB user manual:
> >
> > "Another possible effect of compiler optimizations is to optimize unused
> > variables out of existence, or assign variables to registers (as opposed to
> > memory addresses). Depending on the support for such cases offered by the
> debug
> > info format used by the compiler, GDB might not be able to display values
> for
> > such local variables.
> >
> > To solve such problems, either recompile without optimizations, or use a
> > different debug info format, if the compiler supports several such formats.
> For
> > example, GCC, the GNU C/C++ compiler usually supports the `-gstabs' option.
> > `-gstabs' produces debug info in a format that is superior to formats such
> as
> > COFF. You may be able to use DWARF2 (`-gdwarf-2'), which is also an
> effective
> > form for debug info. See section `Options for Debugging Your Program or GNU
> CC'
> > in Using GNU CC, for more information."
> >
> > I wanted to keep the optimization option, so I tried to recompile with GCC,
> > using the -gstabs and -gdwarf-2 options, and even -g3, with this very
> simple
> > program:
> > int main(void){
> > 	int a = 1;
> > 	int b = 2;
> > 	int c = a+b;
> >
> > 	printf("Value c = %d \n", c);
> > }
> >
> > But still couldn't display the variables a, b and c!
>
> That's because if you ask for the compiler to optimize, it
> will change this program to
>
>       int main(void){
>          printf ("Value c = %d \n", 3");
>       }
>
> if it was a bit cleverer, it might even change it to
>
>       int main(void){
>          printf ("Value c = 3 \n");
>       }
>
> but in either case a,b, and c are gone!
>
> It might be theoretically possible to retain the values of
> a,b,c in the debugging information, but this is a huge amount
> of work, and not something likely to be done in the near future.
>
> > Is there a particular compiling option configuration to set?
>
> Yes,  -O0, if you want junk code kept around for debugging
> purposes, you have to ask for it!
> >
> > Is there a particular compiling option configuration to set?
> >
> > Many thanks in advance.
> >
> > Regards.
>
>




More information about the Gdb mailing list