Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gdb print static variable from function

Tags:

c++

gdb

We have a C++ program such as:

int&
Instance()
{
    static int test;
    return test;
}

int
main( int argc, char ** argv )
{
    int& test = Instance();
    printf("%d\n",test);

    return 0;
}

Now I'm trying to print Instance::test, as suggested by some gdb documentation: ftp://ftp.gnu.org/old-gnu/Manuals/gdb/html_node/gdb_52.html

However, this yields:

(gdb) p Instance::test

No symbol "test" in specified context.

I'm compiling with -O0 -g .

like image 700
Sioux Avatar asked Aug 12 '14 20:08

Sioux


1 Answers

(gdb) p 'Instance()::test'

works for me on Ubuntu 14.04, gdb 7.7, g++ 4.8.2 . How did I know that was the symbol?

$ nm -C a.out | grep test
0000000000601040 d Instance()::test
like image 133
Jordan Samuels Avatar answered Sep 21 '22 03:09

Jordan Samuels