繁体   English   中英

如何在gdb中打印`std :: array`内容?

[英]How to print `std::array` contents in gdb?

我对gdb不太熟悉,无法在gdb手册中找到如何查找这种情况的方法。

我正在尝试在gdb中打印std::array内容。 以下是我尝试在gdb中调试的用例。

template<unsigned int N>
double dotprod(const std::array<double, N> &v1, const std::array<double, N> &v2)
{
     ...
}

在此函数内部,我尝试打印p v1内容。 它打印(const mosp::Point<2u> *) 0x7fffffffc150 如何打印v1的内容?

我一直在与gdb一起使用的一个技巧是定义自己的打印功能。

例如:

void print_int_array(array<int> *a) {
    for (auto it = a->begin(); it != a->end(); ++it)
        cout << *it << endl;
}

然后在gdb提示符下可以运行:

p print_int_array(&array_variable_name)

问题在于,这需要您定义几个函数(注意:gdb可能具有更好的模板支持,并且可以使用带有显式实例化的模板,但是我很保守)

gdb 7.6没有这个问题。

[root@localhost ~]# gdb ./a.out 
GNU gdb (GDB) Fedora (7.6-30.fc19)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it. 
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /root/a.out...done.
(gdb) b main
Breakpoint 1 at 0x80485b9: file a.cpp, line 11. 
(gdb) r
Starting program: /root/a.out 

Breakpoint 1, main () at a.cpp:11
11              std::array<double, 5> a = {0, 1.1, 2.2, 3.3, 4.4};
Missing separate debuginfos, use: debuginfo-install glibc-2.17-4.fc19.i686 libgcc-
4.8.1-1.fc19.i686 libstdc++-4.8.1-1.fc19.i686
(gdb) n
12              std::array<double, 5> b = {5.5, 6.6, 7.7, 8.8, 9.9};
(gdb) 
13              dotprod(a, b); 
(gdb) s
dotprod<5u> (v1=..., v2=...) at a.cpp:7
7               return 0;
(gdb) p v1
$1 = (const std::array<double, 5u> &) @0xbffff640: {_M_elems = {0, 1.1000000000000001, 2.2000000000000002, 
    3.2999999999999998, 4.4000000000000004}}
(gdb)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM