Linux – Cannot see variables in gdb

cygwin;gdblinux

I'm new to cygwin (and *nix for that matter) and don't understand the following gdb behavior.
I've created an executable that intentionally results in a SIGSEGV:

#include <iostream>

void Func(int i)
{
    int* pFoo = NULL;
    *pFoo = 1;
}

int main(int argc, char** argv)
{
    Func(-50);
    std::cout << "End of main()" << std::endl;
}

I compile my code by doing:

g++ test.cpp

Using this version of g++:

g++ –version

g++ (GCC) 5.4.0 Copyright (C) 2015 Free Software Foundation, Inc. This is free software; see the source for copying
conditions. There is NO warranty; not even for MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.

When I try to run this program in gdb, I'm unable to "see" any variables:

>gdb ./a.exe
GNU gdb (GDB) (Cygwin 7.10.1-1) 7.10.1
Copyright (C) 2015 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-pc-cygwin".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./a.exe...done.
(gdb) r
Starting program: /home/user/code/gdbtest/a.exe
[New Thread 6500.0x1c84]
[New Thread 6500.0x7a0]

Program received signal SIGSEGV, Segmentation fault.
0x000000010040111d in Func(int) ()
(gdb) bt
#0  0x000000010040111d in Func(int) ()
#1  0x000000010040113c in main ()
(gdb) f 0
#0  0x000000010040111d in Func(int) ()
(gdb) p i
No symbol "i" in current context.
(gdb) p pFoo
No symbol "pFoo" in current context.
(gdb) info locals
No symbol table info available.
(gdb) f 1
#1  0x000000010040113c in main ()
(gdb) p argc
No symbol "argc" in current context.
(gdb)

Can someone explain why this happens and how I can correct the problem?

I also tried compiling using "g++ -Og test.cpp" but it resulted in no change to the above described.

Best Answer

General note : is a bad idea to call a program test, as there is already a test command.

Changing name ;-)

$ g++ -ggdb -O0 prova.cpp -o prova

Where

-ggdb is the option for adding the debug info
-O0 disable all the optimization
-o provide a name to the binary other than the default a.exe

The debugging section will be:

$ gdb prova
GNU gdb (GDB) (Cygwin 7.10.1-1) 7.10.1
...
Reading symbols from prova...done.
(gdb) run
Starting program: /tmp/prova/prova
[New Thread 6404.0x404]
[New Thread 6404.0x231c]
[New Thread 6404.0x1960]
[New Thread 6404.0x11b4]

Program received signal SIGSEGV, Segmentation fault.
0x00000001004010f7 in Func (i=-50) at prova.cpp:6
6           *pFoo = 1;
(gdb) bt
#0  0x00000001004010f7 in Func (i=-50) at prova.cpp:6
#1  0x0000000100401122 in main (argc=1, argv=0xffffcc30) at prova.cpp:11
Related Question