检查gdb中的模板参数包

时间:2016-04-04 16:23:24

标签: c++ gcc gdb variadic-templates

我正在尝试调试以下简单程序:

#include <iostream>

template <class... Args>
void printAll(Args&&... args) {
    using swallow = int[];
    swallow{0,
        (std::cout << args, 0)...
    };  
}

int main() {
    printAll(1, "23", 4); 
}

使用gcc 4.9.2编译:

g++ -std=c++11 -g -O0 foo.cxx

然后使用:

使用gdb 7.9进行调试
gdb a.out

(gdb) break foo.cxx:5
Breakpoint 1 at 0x400884: file foo.cxx, line 5.
(gdb) run
Starting program: /..[snip]../a.out 

Breakpoint 1, printAll<int, char const (&) [3], int>(int&&, char const (&) [3], int&&) () at foo.cxx:6
6       swallow{0,
(gdb) bt
#0  printAll<int, char const (&) [3], int>(int&&, char const (&) [3], int&&) () at foo.cxx:6
#1  0x0000000000400813 in main () at foo.cxx:12

我的功能正常,但我无法检查参数包:

(gdb) info args
No arguments.
(gdb) print args
No symbol "args" in current context.
(gdb) inspect args
No symbol "args" in current context.

我如何实际检查参数?

1 个答案:

答案 0 :(得分:4)

Related: Showing values of parameters packs in gdb

There are two problems here; the first is that g++ emits parameter pack debugging information in the DWARF format using the tags DW_TAG_GNU_template_parameter_pack and DW_TAG_GNU_formal_parameter_pack, which gdb does not yet support (patch attached).

Even when this is fixed, we run into another problem, which is that the debugging information g++ emits is broken; it's missing the parameter name (DW_AT_name) (patch attached).

TBH gdb support for C++11 is pretty abysmal (unsurprising as it was effectively abandoned for so long); another near-showstopper bug for C++11 is that it doesn't support rvalue references (DW_TAG_rvalue_reference_type) (patch attached), printing error messages like <unknown type in /tmp/a.out, CU 0x0, DIE 0x7f>.

The workaround (other than using clang, or an ancient version of g++ that doesn't use the DW_TAG_GNU_template_parameter_pack tags, e.g. 4.4.7) is to use the stabs debugging format with GCC extensions:

g++ -std=c++11 -gstabs+ -O0 foo.cxx

(gdb) s
void printAll<int, char const (&) [3], int>(int, int&&, char const (&) [3], int&&) (i=999, args#0=@0x7fffffffe45c: 1, args#1=..., args#2=@0x7fffffffe458: 4)
    at p.cpp:7
7       swallow{0,
(gdb) p 'args#0'
$1 = (int &) @0x7fffffffe45c: 1