簡體   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