Inspect template parameter pack in gdb

冷暖自知 提交于 2019-12-19 07:23:40

问题


I'm trying to debug the following simple program:

#include <iostream>

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

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

Compiled with gcc 4.9.2 using:

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

And then debugging with gdb 7.9 using:

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

I'm in the right function, but I have no way to inspect the parameter pack:

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

How do I actually examine the arguments?


回答1:


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


来源:https://stackoverflow.com/questions/36407941/inspect-template-parameter-pack-in-gdb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!