我有一个简单的模板类:
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;
};
}
当我在gdb中调试时,我想看到_db_internal容器,但不知道如何访问它。
我尝试用gdb编写:
p 'test::DB<std::string, someclass*, std::tr1::unordered_map< std::string, someclass* > >::instance()::_instance'._db_internal
它给了我:当前背景下没有符号......
也试过没有单引号但没有运气。
如何在gdb中打印该容器? 我正在使用gdb版本:7.6.1
由于
正如使用gdb自动完成建议我能够得到这个:
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'
但这给了我0不好
然后如果我尝试了:
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'
我也有错误说:
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.
答案 0 :(得分:4)
但这给了我0不好
这似乎是GDB中的一个错误,几天前仍然存在于使用git构建的GDB中。这有效:
(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>
作为实例,因为我必须完成你可以编译和运行的程序的代码片段。 (你应该提供了一个完整的程序,但没有。)