Inspecting standard container (std::map) contents with gdb

Paolo Tedesco picture Paolo Tedesco · Jan 9, 2009 · Viewed 92.6k times · Source

Supposing to have something like this:

#include <map>
int main(){
    std::map<int,int> m;
    m[1] = 2;
    m[2] = 4;
    return 0;
}

I would like to be able to inspect the contents of the map running the program from gdb.
If I try using the subscript operator I get:

(gdb) p m[1]
Attempt to take address of value not located in memory.

Using the find method does not yield better results:

(gdb) p m.find(1)
Cannot evaluate function -- may be inlined

Is there a way to accomplish this?

Answer

Jonathan Wakely picture Jonathan Wakely · Mar 11, 2013

The existing answers to this question are very out of date. With a recent GCC and GDB it Just WorksTM thanks to the built-in Python support in GDB 7.x and the libstdc++ pretty printers that come with GCC.

For the OP's example I get:

(gdb) print m
$1 = std::map with 2 elements = {[1] = 2, [2] = 4}

If it doesn't work automatically for you see the first bullet point on the STL Support page of the GDB wiki.

You can write Python pretty printers for your own types too, see Pretty Printing in the GDB manual.