Print a static variable from a member function of a template class in gdb

I have a simple class of templates:

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 gdb, I want to see the _db_internal container, but I 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 this gives me: no character ... in the current context

also tried without single quotes, but no luck.

How to print this container in gdb? I am using gdb version: 7.6.1

thank

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 it 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 received an error message:

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
source share
1 answer

but it gives me 0, which is not good

, GDB, GDB, git . :

(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

. , _instance:

(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

... 0x00000000006020a0 :

(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>

, :

(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. <int,int> , , . ( , ).

+3

Source: https://habr.com/ru/post/1655960/


All Articles