GDB有没有办法在不省略模板参数的情况下打印类型



似乎对于几个 STL 容器,GDB 省略了打印其模板参数。例如

(gdb) whatis a
type = std::vector<int>

这给我带来了问题。

(gdb) whatis std::vector<int>::_M_impl
No type "vector<int>" within class or namespace "std".
(gdb) p *reinterpret_cast<std::vector<int>*>(0x7fffffffd920)
A syntax error in expression, near `*>(0x7fffffffd920)'.

为了得到我想要的,我必须手动添加未显示的模板参数。

(gdb) whatis std::vector<int, std::allocator<int> >::_M_impl
type = std::_Vector_base<int, std::allocator<int> >::_Vector_impl
(gdb) p *reinterpret_cast<std::vector<int, std::allocator<int> >*>(0x7fffffffd920)
$5 = ......

但是,这并不理想,因为很难通用程序添加这些省略的模板参数。例如,给定std::map<int, double>,我怎么知道有额外的模板参数CompareAllocator,从而能够得到std::less<Key>std::allocator<std::pair<const Key, T> >

GDB 有没有办法在不省略模板参数的情况下打印类型?还是有其他方法可以解决我的问题?

GDB 有没有办法在不省略模板参数的情况下打印类型?

使用 TAB 完成。例:

$ cat t.cc
#include <map>
int main()
{
  std::map<char, int> m = {{'a', 1}, {'z', 2}};
  return 0;
}
$ g++ -g t.cc && gdb -q ./a.out
(gdb) start
Temporary breakpoint 1 at 0xa87: file t.cc, line 5.
Starting program: /tmp/a.out
Temporary breakpoint 1, main () at t.cc:5
5     std::map<char, int> m = {{'a', 1}, {'z', 2}};
(gdb) n
6     return 0;
(gdb) p 'std::map<TAB>   # Note: single quote is important here.

完成到:

(gdb) p 'std::map<char, int, std::less<char>, std::allocator<std::pair<char const, int> > >

现在您可以完成:

(gdb) p ('std::map<char, int, std::less<char>, std::allocator<std::pair<char const, int> > >' *)&m
$1 = (std::map<char, int, std::less<char>, std::allocator<std::pair<char const, int> > > *) 0x7fffffffdb60

最后:

(gdb) p *$1
$2 = std::map with 2 elements = {[97 'a'] = 1, [122 'z'] = 2}

相关内容

最新更新