Cannot see variables in gdb

9,553

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
Share:
9,553

Related videos on Youtube

StoneThrow
Author by

StoneThrow

Updated on September 18, 2022

Comments

  • StoneThrow
    StoneThrow almost 2 years

    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.

  • StoneThrow
    StoneThrow almost 8 years
    I will be able to test your suggestion in a few hours. Before then, though: the link you sent states "gcc, the gnu C/C++ compiler, supports ‘-g’ with or without ‘-O’, making it possible to debug optimized code." In my original post, I noted that I also compiled with "g++ -Og test.cpp". Can you account for this discrepancy? A second question: on a true *nix system at work (vs. cygwin of the original post), I've compiled with g++ without an explicit "-g", yet gdb is able to see symbols in that environment. g++ is not aliased to "g++ -g". Do you know why that discrepancy may be? Thank you.
  • StoneThrow
    StoneThrow almost 8 years
    This worked to (as well as just "-g"). Still curious why "-Og" did not work, as my reading of the manual led me to think it ought to have. Also curious why neither "-g" nor "-ggdb" are required on my work PC (which is true *nix). Just curious on these points, but you've solved my query; thank you.
  • MAP
    MAP almost 8 years
    You should click the check mark by the answer to tell the computer that. It can't understand the text, and the check makes it rate this question higher when people are searching the Q&A.