238

I want to examine the contents of a std::vector in GDB, how do I do it? Let's say it's a std::vector<int> for the sake of simplicity.

John Carter
  • 53,924
  • 26
  • 111
  • 144
  • 3
    Similar question: http://stackoverflow.com/questions/427589/inspecting-standard-container-stdmap-contents-with-gdb (the link in the answer is very interesting). – Paolo Tedesco Jun 26 '09 at 15:47
  • 1
    The new, better way to do this is in this question: http://stackoverflow.com/questions/2492020/how-to-view-contents-of-stl-containers-using-gdb-7-x/2492341#2492341 – dshepherd Apr 19 '13 at 11:19
  • Or [c++ - How to pretty-print STL containers in GDB? - Stack Overflow](https://stackoverflow.com/questions/11606048/how-to-pretty-print-stl-containers-in-gdb) – user202729 Dec 04 '21 at 07:20

5 Answers5

292

With GCC 4.1.2, to print the whole of a std::vector<int> called myVector, do the following:

print *(myVector._M_impl._M_start)@myVector.size()

To print only the first N elements, do:

print *(myVector._M_impl._M_start)@N

Explanation

This is probably heavily dependent on your compiler version, but for GCC 4.1.2, the pointer to the internal array is:

myVector._M_impl._M_start 

And the GDB command to print N elements of an array starting at pointer P is:

print P@N

Or, in a short form (for a standard .gdbinit):

p P@N
gallo
  • 546
  • 7
  • 10
John Carter
  • 53,924
  • 26
  • 111
  • 144
  • 4
    Hehe, it's something that's bugged me before, so I just looked it up this morning and added it as a memo to myself (as Jeff himself recommended). – John Carter Oct 31 '08 at 11:10
  • 3
    Also if you want just a particular vector element, myVector._M_impl._M_start + n (for the nth element) – mariner Jan 07 '14 at 01:26
  • 4
    Not working for me. `Cannot evaluate function -- may be inlined` – wallyk Apr 30 '15 at 17:29
  • 2
    To print a single element, e.g. the 2nd element: print (myVector._M_impl._M_start)[2] – jfritz42 Aug 31 '15 at 22:08
  • 2
    To find the special names (`_M_impl` etc) for your compiler under GDB 7.0+, use `print /r myVector` – Eponymous Nov 30 '15 at 17:53
  • 1
    with GCC 5.2.0 (though I am sure it works for earlier versions), you can use `print *(&myVector[0])@myVector.size()`, which requires no specific knowledge of GCC stdlib internals. – bcumming Dec 01 '15 at 10:34
  • Thanks @Eponymous !! I was wondering how to arrive at the right inner member names. – agam May 23 '19 at 19:22
  • Not sure how much it helps in debugability, but in addition to this tip I also compiled with `g++ -ggdb -O0` – daparic Oct 20 '19 at 05:23
85

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

user202729
  • 3,358
  • 3
  • 25
  • 36
  • 20
    This works fine as long as the vector elements are directly interpretable. But it doesn't help if the vector contains pointers to the items of interest. – wallyk Apr 30 '15 at 17:34
  • I frankly don't find the [gdb wiki](http://sourceware.org/gdb/wiki/STLSupport) page particularly readable, maybe because it's "slightly" outdated now? For instance, I had the impression that the suggested content of the `$HOME/.gdbinit` was necessary. At the moment I end up with no such file at all and `gdb` correctly showing the content of `std::vector`. However, since during my "rambling" attempts I just installed and then unistalled `cgdb`, and I already had `libstdc++5` installed, I have no idea why the pretty printing did not work while now it works. – Enlico Jan 05 '20 at 13:38
  • Why is this the accepted answer? This does not work at all. – Philipp Ludwig Jan 03 '23 at 09:47
15

put the following in ~/.gdbinit

define print_vector
    if $argc == 2
        set $elem = $arg0.size()
        if $arg1 >= $arg0.size()
            printf "Error, %s.size() = %d, printing last element:\n", "$arg0", $arg0.size()
            set $elem = $arg1 -1
        end
        print *($arg0._M_impl._M_start + $elem)@1
    else
        print *($arg0._M_impl._M_start)@$arg0.size()
    end
end

document print_vector
Display vector contents
Usage: print_vector VECTOR_NAME INDEX
VECTOR_NAME is the name of the vector
INDEX is an optional argument specifying the element to display
end

After restarting gdb (or sourcing ~/.gdbinit), show the associated help like this

gdb) help print_vector
Display vector contents
Usage: print_vector VECTOR_NAME INDEX
VECTOR_NAME is the name of the vector
INDEX is an optional argument specifying the element to display

Example usage:

(gdb) print_vector videoconfig_.entries 0
$32 = {{subChannelId = 177 '\261', sourceId = 0 '\000', hasH264PayloadInfo = false, bitrate = 0,     payloadType = 68 'D', maxFs = 0, maxMbps = 0, maxFps = 134, encoder = 0 '\000', temporalLayers = 0 '\000'}}
badeip
  • 602
  • 8
  • 9
  • 2
    thank you for the code! I guess there is a typo and "print *($arg0._M_impl._M_start + $elem)@1" should be "print *($arg0._M_impl._M_start + $arg1)@1"? I use the following modification: define print_vector if $argc == 2 if $arg1 >= $arg0.size()-1 printf "Error, %s.size() = %d, printing last element:\n", "$arg0", $arg0.size()-1 end print *($arg0._M_impl._M_start + $arg1)@1 else print *($arg0._M_impl._M_start)@$arg0.size() end end – user1541776 Apr 17 '17 at 08:46
  • el magnifico! mochas gracias – daparic Oct 20 '19 at 05:44
13

'Watching' STL containers while debugging is somewhat of a problem. Here are 3 different solutions I have used in the past, none of them is perfect.

1) Use GDB scripts from http://clith.com/gdb_stl_utils/ These scripts allow you to print the contents of almost all STL containers. The problem is that this does not work for nested containers like a stack of sets.

2) Visual Studio 2005 has fantastic support for watching STL containers. This works for nested containers but this is for their implementation for STL only and does not work if you are putting a STL container in a Boost container.

3) Write your own 'print' function (or method) for the specific item you want to print while debugging and use 'call' while in GDB to print the item. Note that if your print function is not being called anywhere in the code g++ will do dead code elimination and the 'print' function will not be found by GDB (you will get a message saying that the function is inlined). So compile with -fkeep-inline-functions

Gustavo Maciel
  • 652
  • 7
  • 20
Nikhil
  • 2,230
  • 6
  • 33
  • 51
3

A little late to the party, so mostly a reminder to me next time I do this search!

I have been able to use:

p/x *(&vec[2])@4

to print 4 elements (as hex) from vec starting at vec[2].

Mike P
  • 742
  • 11
  • 26