【问题标题】:Is there a way for GDB to print type without omitting template parameters?GDB 有没有办法在不省略模板参数的情况下打印类型?
【发布时间】:2018-12-23 04:49:32
【问题描述】:

似乎对于几个 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&lt;int, double&gt;,我怎么知道有多余的模板参数CompareAllocator,从而能够得到std::less&lt;Key&gt;std::allocator&lt;std::pair&lt;const Key, T&gt; &gt;

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

【问题讨论】:

  • @Jans 怎么是重复的?

标签: c++ gdb


【解决方案1】:

有没有办法让 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}

【讨论】:

    猜你喜欢
    • 2019-03-29
    • 2014-07-11
    • 1970-01-01
    • 1970-01-01
    • 2011-06-12
    • 1970-01-01
    • 2022-11-25
    • 2012-10-11
    • 1970-01-01
    相关资源
    最近更新 更多