13

I have been using the macro solution, as it is outlined here. However, there is a mention on how to view them without macros. I am referring to GDB version 7 and above.

Would someone illustrate how?

Thanks

Community
  • 1
  • 1
vehomzzz
  • 42,832
  • 72
  • 186
  • 216

2 Answers2

22

Get the python viewers from SVN

svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python

Add the following to your ~/.gdbinit

python
import sys
sys.path.insert(0, '/path/to/pretty-printers/dir')
from libstdcxx.v6.printers import register_libstdcxx_printers
register_libstdcxx_printers (None)
end

Then print should just work:

std::map<int, std::string> the_map;
the_map[23] = "hello";
the_map[1024] = "world";

In gdb:

(gdb) print the_map 
$1 = std::map with 2 elements = { [23] = "hello", [1024] = "world" }

To get back to the old view use print /r (/r is for raw).

See also: http://sourceware.org/gdb/wiki/STLSupport

dshepherd
  • 4,989
  • 4
  • 39
  • 46
McBeth
  • 1,177
  • 8
  • 12
  • And how to actually use it in GDB to print the contents? Is anything need to be set. thx – vehomzzz Mar 23 '10 at 12:18
  • Once that load code is in .gdbinit, then print should just work, to get back to the old view, you do a print /r (/r is for raw) (formatting apparently sucks in comments, my apologies) std::map the_map; the_map[23] = "hello"; the_map[1024] = "world"; (gdb) print the_map $1 = std::map with 2 elements = { [23] = "hello", [1024] = "world" } – McBeth Mar 23 '10 at 13:01
  • 1
    Consider editing your answer to include this information in your comment. That way the formatting wont suck. – camh Apr 21 '10 at 13:18
  • 1
    please put this code sample in the answer. I can't read it in the comments. – Nathan Fellman Aug 26 '10 at 13:08
2

The libstdcxx_printers are included with recent versions of GCC, so if you're using GCC 4.5 or later then you don't need to do anything, pretty printing Just Works.

(gdb) p v
$1 = std::vector of length 3, capacity 3 = {std::set with 3 elements = {
    [0] = 1, [1] = 2, [2] = 3}, std::set with 2 elements = {[0] = 12, 
    [1] = 13}, std::set with 1 elements = {[0] = 23}}
(gdb) p v[1]
$2 = std::set with 2 elements = {[0] = 12, [1] = 13}

To disable the pretty printing use p/r or print/r to get "raw" output.

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521