How do I print the elements of a C++ vector in GDB?
To view vector std::vector myVector contents, just type in GDB:
(gdb) print myVector
This will produce an output similar to:
$1 = std::vector of length 3, capacity 4 = {10, 20, 30}
To achieve above, you need to have gdb 7 (I tested it on gdb 7.01) and some python pretty-printer. Installation process of these is described on gdb wiki.
What is more, after installing above, this works well with Eclipse C++ debugger GUI (and any other IDE using GDB, as I think).
How do I examine the contents of an std::vector in gdb, using the icc compiler?
Not sure this will work with your vector, but it worked for me.
#include <string>
#include <vector>
int main() {
std::vector<std::string> vec;
vec.push_back("Hello");
vec.push_back("world");
vec.push_back("!");
return 0;
}
gdb:
(gdb) break source.cpp:8
(gdb) run
(gdb) p vec.begin()
$1 = {
_M_current = 0x300340
}
(gdb) p $1._M_current->c_str()
$2 = 0x3002fc "Hello"
(gdb) p $1._M_current +1
$3 = (string *) 0x300344
(gdb) p $3->c_str()
$4 = 0x30032c "world"
How to watch the size of a C++ std::vector in gdb?
Is there a way to "watch" the contents (or size) of a std::vector from gdb?
Assuming you are using GCC, set watchpoints on theVector->_M_impl._M_start
and _M_finish
. If you are using some other std::vector implementation, adjust accordingly.
Example:
#include <vector>
int main()
{
std::vector<int> v;
v.push_back(1);
v.push_back(2);
}
g++ -g t.cc
gdb -q ./a.out
Reading symbols from /tmp/a.out...done.
(gdb) start
Temporary breakpoint 1 at 0x40090f: file t.cc, line 5.
Temporary breakpoint 1, main () at t.cc:5
5 std::vector<int> v;
(gdb) n
7 v.push_back(1);
(gdb) p v._M_impl._M_start
$1 = (int *) 0x0
(gdb) p v._M_impl._M_finish
$2 = (int *) 0x0
(gdb) p &v._M_impl._M_finish
$3 = (int **) 0x7fffffffd878
(gdb) watch *$3
Hardware watchpoint 2: *$3
(gdb) p &v._M_impl._M_start
$4 = (int **) 0x7fffffffd870
(gdb) watch *$4
Hardware watchpoint 3: *$4
(gdb) c
Hardware watchpoint 3: *$4
Old value = (int *) 0x0
New value = (int *) 0x604010
std::vector<int, std::allocator<int> >::_M_insert_aux (this=0x7fffffffd870, __position=0x0) at /usr/include/c++/4.4/bits/vector.tcc:365
365 this->_M_impl._M_finish = __new_finish;
(gdb) bt
#0 std::vector<int, std::allocator<int> >::_M_insert_aux (this=0x7fffffffd870, __position=0x0) at /usr/include/c++/4.4/bits/vector.tcc:365
#1 0x0000000000400a98 in std::vector<int, std::allocator<int> >::push_back (this=0x7fffffffd870, __x=@0x7fffffffd88c) at /usr/include/c++/4.4/bits/stl_vector.h:741
#2 0x0000000000400935 in main () at t.cc:7
(gdb) c
Hardware watchpoint 2: *$3
Old value = (int *) 0x0
New value = (int *) 0x604014
std::vector<int, std::allocator<int> >::_M_insert_aux (this=0x7fffffffd870, __position=0x0) at /usr/include/c++/4.4/bits/vector.tcc:366
366 this->_M_impl._M_end_of_storage = __new_start + __len;
(gdb) bt
#0 std::vector<int, std::allocator<int> >::_M_insert_aux (this=0x7fffffffd870, __position=0x0) at /usr/include/c++/4.4/bits/vector.tcc:366
#1 0x0000000000400a98 in std::vector<int, std::allocator<int> >::push_back (this=0x7fffffffd870, __x=@0x7fffffffd88c) at /usr/include/c++/4.4/bits/stl_vector.h:741
#2 0x0000000000400935 in main () at t.cc:7
... etc...
Print the values of variables of a structure array in GDB
I did not want to write a loop each time I launch GDB, and pretty-printers rely on Python which does not fit me. Printing the value of variables from non contiguous memory spaces seems not to be easily feasible in GDB.
I end up finding an alternative way which seems to be the easiest one. I wrote a C function in the code to print the values I need to get:
void print(projection_t *projections, int size)
{
int i;
for(i=0; i<size; i++)
printf("proj_%d: size=%d\n", i, projections[i].size);
}
It is possible to call the function from GDB when I want to print the size of each projections:
(gdb) call print(projections, len)
proj_0: size=1027
proj_1: size=1024
proj_2: size=1027
proj_3: size=1030
How to inspect an individual element in a c++ map using GDB and access members of an object pointed by the element in the map
Can you please let me know how this is done in GDB?
You could always do this: (gdb) p *(Parent*) 0xa85ed0
.
GDB print all values in char array
You can use x/999bc
, where 999
is the size of your array, for instance:
paul@thoth:~/src/sandbox$ gdb ./str
GNU gdb (GDB) 7.4.1-debian
Copyright (C) 2012 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 "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/paul/src/sandbox/str...done.
(gdb) list
1 int main(void) {
2 char * p = "hello\0world\0hahaha";
3 return 0;
4 }
5
(gdb) b 3
Breakpoint 1 at 0x4004b8: file str.c, line 3.
(gdb) run
Starting program: /home/paul/src/sandbox/str
Breakpoint 1, main () at str.c:3
3 return 0;
(gdb) print p
$1 = 0x40056c "hello"
(gdb) x/19bc p
0x40056c: 104 'h' 101 'e' 108 'l' 108 'l' 111 'o' 0 '\000' 119 'w' 111 'o'
0x400574: 114 'r' 108 'l' 100 'd' 0 '\000' 104 'h' 97 'a' 104 'h' 97 'a'
0x40057c: 104 'h' 97 'a' 0 '\000'
(gdb)
Related Topics
Purpose of Trigraph Sequences in C++
Convert Mat to Array/Vector in Opencv
Why Use Functors Over Functions
How to Read a Binary File into a Vector of Unsigned Chars
Size of Array Passed to C++ Function
C++ Overload Static Function with Non-Static Function
C++ Warning: Deprecated Conversion from String Constant to 'Char*' [-Wwrite-Strings]
How to Use C++ Classes with Ctypes
How to Simulate "Press Any Key to Continue"
Difference Between Char* and Char[]
How to Use Sdl2 and Sdl_Image with Cmake
Why Can't You Use Offsetof on Non-Pod Structures in C++
Force Gcc to Notify About Undefined References in Shared Libraries
How to Measure CPU Time of a Specific Set of Threads