简体   繁体   中英

How to print `std::array` contents in gdb?

I am not well familiar with gdb, and couldn't figure how to look for this scenario in gdb manual.

I am trying to print contents of std::array in gdb. Below is the usecase I am trying to debug in gdb.

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

While inside this function I tried to print contents of p v1 . It prints (const mosp::Point<2u> *) 0x7fffffffc150 . How can I print the contents of v1 ?

A trick that I've been using with gdb is defining my own printing functions.

For example:

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

Then from gdb prompt you can run:

p print_int_array(&array_variable_name)

The problem is that this requires you to define several functions (note: gdb might have better template support and you can use templates with explicit instantiation, but i'm conservative)

gdb 7.6 no this problem.

[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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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