This is the mail archive of the
gdb@sources.redhat.com
mailing list for the GDB project.
Re: c++ stl containers
- From: mec dot gnu at mindspring dot com (Michael Elizabeth Chastain)
- To: gdb at sources dot redhat dot com, luis_solis at terra dot es
- Date: Sat, 10 Jan 2004 18:00:36 -0500 (EST)
- Subject: Re: c++ stl containers
Just do: print xy[0], xy[1], and so on. gdb turns the '[0]'
into a C++ method call just like the C++ compiler does.
See the attached typescript for an example.
Unfortunately, "print xy.size()" does not work because the test program
does not call the "size" method. The template function is present in
the executable only if the program uses it. If the test program calls
std::vector<float>::size somewhere, then "print xy.size()" will work in
gdb.
You need gdb 6.0, and you probably need a recent gcc.
I use gcc 3.3.2. And compile and link with -gdwarf-2.
-gstabs+ or just plain -g might work.
Hope this helps,
Michael C
GDB QA Guy
Script started on Sat Jan 10 17:44:59 2004
[mec.gnu@berman tmp]$ cat vf.cc
#include <vector>
void foo () { ; }
int main ()
{
std::vector <float> xy(2);
xy[0] = 2.0;
xy[1] = 3.0;
foo ();
}
[mec.gnu@berman tmp]$ /berman/migchain/install/target/native/gcc-3.3.2-as-2.14-ldgd-2.14/bin/g++ -gdwarf-2 vf.cc
[mec.gnu@berman tmp]$ /berman/migchain/install/target/native/gdb-6.0/bin/gdb a.out
GNU gdb 6.0
Copyright 2003 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i686-pc-linux-gnu"...
(gdb) break main
Breakpoint 1 at 0x804995b: file vf.cc, line 7.
(gdb) run
Starting program: /berman/home/mec.gnu/tmp/a.out
Breakpoint 1, main () at vf.cc:7
7 std::vector <float> xy(2);
(gdb) next
8 xy[0] = 2.0;
(gdb)
9 xy[1] = 3.0;
(gdb)
10 foo ();
(gdb) print xy[0]
$1 = (float &) @0x80a9bb0: 2
(gdb) print xy[1]
$2 = (float &) @0x80a9bb4: 3
(gdb) print xy.size()
Cannot evaluate function -- may be inlined
(gdb) print xy
$3 = {<_Vector_base<float,std::allocator<float> >> = {<_Vector_alloc_base<float,std::allocator<float>,true>> = {_M_start = 0x80a9bb0, _M_finish = 0x80a9bb8,
_M_end_of_storage = 0x80a9bb8}, <No data fields>}, <No data fields>}
(gdb) quit
The program is running. Exit anyway? (y or n) y
[mec.gnu@berman tmp]$ exit
exit
Script done on Sat Jan 10 17:47:25 2004