6

I have a simple template class:

namespace test
{

template< class Key, class Value, class Container = std::map< Key, Value > >
class DB
{
public:

    static DB& instance()
    {
        static DB _instance;

        return _instance;
    }
private:
    DB(){};
    DB( DB const& ){};
    void operator=( DB const& ){};

    Container _db_internal;
};
}

When I debug in gdb I want to see the _db_internal container, but don't know how to access it.

I tried writing in gdb:

p 'test::DB<std::string, someclass*, std::tr1::unordered_map< std::string, someclass* > >::instance()::_instance'._db_internal

and it gives me: No symbol ... in current context

also tried without single quotes but no luck.

How to print that container in gdb? I'm using gdb version: 7.6.1

Thanks

As suggested using gdb autocomplete I was able to get this:

p 'test::DB<std::string, std::string, std::map<std::string, std::string, std::less<std::string>, std::allocator<std::pair<std::string const, std::string> > > >::instance()::_instance'

but that gives me 0 which is not good

then if I tried:

p 'test::DB<std::string, std::string, std::map<std::string, std::string, std::less<std::string>, std::allocator<std::pair<std::string const, std::string> > > >::instance()::_instance._db_internal' 

I also got an error saying:

No symbol "test::DB<std::string, std::string, std::map<std::string, std::string, std::less<std::string>, std::allocator<std::pair<std::string const, std::string> > > >::instance()::_instance._db_internal" in current context.
4
  • Have you tried this: p 'test::DB<std::string, someclass*, std::tr1::unordered_map< std::string, someclass* > >::instance()._db_internal'? Commented Sep 27, 2016 at 12:12
  • it is the same error: No symbol ... Commented Sep 27, 2016 at 12:14
  • Have you tried using gdb's autocomplete? Commented Sep 27, 2016 at 12:15
  • Perhaps try 'auto tmp = test::DB.....' and then you will have a symbol tmp, which is what GDB is complaining about. Commented Sep 28, 2016 at 1:20

1 Answer 1

5

but that gives me 0 which is not good

That seems to be a bug in GDB, still present in GDB built from git as of a few days ago. This works:

(gdb) start
Temporary breakpoint 1 at 0x400865: file foo.cc, line 27.
Starting program: /tmp/a.out 
Temporary breakpoint 1, main () at foo.cc:27
27    auto& db = test::DB<int, int>::instance();
(gdb) n
28    return 0;
(gdb) p 'test::DB<int, int, std::map<int, int, std::less<int>, std::allocator<std::pair<int const, int> > > >::instance()::_instance'
$1 = 0

That reproduced your behavior. Let's find out where the _instance resides:

(gdb) info var _instance
All variables matching regular expression "_instance":

Non-debugging symbols:
0x0000000000602088  guard variable for test::DB<int, int, std::map<int, int, std::less<int>, std::allocator<std::pair<int const, int> > > >::instance()::_instance
0x00000000006020a0  test::DB<int, int, std::map<int, int, std::less<int>, std::allocator<std::pair<int const, int> > > >::instance()::_instance

... and interpret the address 0x00000000006020a0 as a pointer to desired type:

(gdb) p ('test::DB<int, int, std::map<int, int, std::less<int>, std::allocator<std::pair<int const, int> > > >' *)0x00000000006020a0
$2 = (test::DB<int, int, std::map<int, int, std::less<int>, std::allocator<std::pair<int const, int> > > > *) 0x6020a0 <test::DB<int, int, std::map<int, int, std::less<int>, std::allocator<std::pair<int const, int> > > >::instance()::_instance>

Finally we can dereference it:

(gdb) p *$
$3 = {_db_internal = {_M_t = {_M_impl = {<std::allocator<std::_Rb_tree_node<std::pair<int const, int> > >> = {<__gnu_cxx::new_allocator<std::_Rb_tree_node<std::pair<int const, int> > >> = {<No data fields>}, <No data fields>}, 
        _M_key_compare = {<std::binary_function<int, int, bool>> = {<No data fields>}, <No data fields>}, _M_header = {_M_color = std::_S_red, _M_parent = 0x0, 
          _M_left = 0x6020a8 <test::DB<int, int, std::map<int, int, std::less<int>, std::allocator<std::pair<int const, int> > > >::instance()::_instance+8>, 
          _M_right = 0x6020a8 <test::DB<int, int, std::map<int, int, std::less<int>, std::allocator<std::pair<int const, int> > > >::instance()::_instance+8>}, _M_node_count = 0}}}}

P.S. I used <int,int> as the instance, since I had to complete your snippet to a program I can compile and run. (You should have provided a complete program, but didn't).

Sign up to request clarification or add additional context in comments.

1 Comment

I was afraid that this will be the answer, but it seems like there is nothing better you can do with gdb right now. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.