2

I am trying to view the contents of C++ STL Containers. I read here that the recent versions of gcc and gdb support pretty printing by default but when I tried to display a map I got the following:

enter image description here

Then, I tried to set up pretty printing according to this answer but still it shows the same output. Can someone please help me out? Thanks.

I am using Ubuntu 16.04, g++ 5.4.1, GDB 7.11.1 and python 2.7.12 (installed from their official sources).

Community
  • 1
  • 1
piyukr
  • 631
  • 3
  • 12
  • 18
  • Did you follow [this](http://sourceware.org/gdb/wiki/STLSupport) guide? Is the assumption correct that you're running Linux and not using eclipse, so Windows settings and eclipse compatibilities can be excluded as possible reasons? – starturtle Sep 28 '16 at 05:11
  • I am using Ubuntu 16.04. Yes, I have followed this guide. Still I get the output as shown above :( – piyukr Sep 28 '16 at 05:12
  • It's not clear from your question whether you are *using* GDB that came with your distribution, or *building* your own. If the former, it should just work. If the latter, you probably didn't build it correctly. – Employed Russian Sep 28 '16 at 05:33
  • 1
    Can you show us any python init files you've added as well as the pathname of your gdb installation directory? Typically on Ubuntu, all you need to do to get gdb to load the C++ pretty printers is to symlink `yourinstalldir/share/gdb/auto-load` (which doesn't exist in a fresh gdb installation) to `/usr/share/gdb/auto-load` . The `auto-load` directory tree, and the pretty printers, are supplied by the `libstdc++6` package on Ubuntu. – Mark Plotnick Sep 28 '16 at 14:06

3 Answers3

3

It was stated in the other thread you mentioned that some versions of GDB work with Python 3 only.

That can be ruled out as possible reasons since you don't receive any errors as far as your description says.

Over at sourceware.org it is suggested that

You can extend gdb using the Python programming language. This feature is available only if gdb was configured using --with-python.

Not sure if you did because the guides you used don't mention it directly.

Community
  • 1
  • 1
starturtle
  • 699
  • 8
  • 25
  • Hi, I tried running `gdb a.out --with-python ` , it showed `gdb: unrecognized option '--with-python' ` – piyukr Sep 28 '16 at 05:28
  • 1
    @piyukr You pass `--with-python` to GDB `configure` script. Once you've built GDB, it's too late. – Employed Russian Sep 28 '16 at 05:32
  • I tried to build gdb again, this time with _./configure --with-python_ -> `rm -r \usr\bin\gdb` -> `make` -> `make install` but I got [this](http://imgur.com/a/CCVKC) . Can you please tell me how should fix this? – piyukr Sep 28 '16 at 07:28
  • 1
    The error message in your Imgur link says you should install `makeinfo`. For Ubuntu, `apt install texinfo` should accomplish that. – Eirik Fuller Sep 28 '16 at 13:31
3

Mark was correct. I built my own version of gdb 8.0 with --with-python option but still I was not able to see container contents. gdb was showing all complex structure. So I followed Mark's suggestion.

You need to create a soft link (auto-load) in your gdb install directory. If your gdb install directory is $HOME/gdb_install then

cd $HOME/gdb_install/share/gdb

ln -s /usr/share/gdb/auto-load auto-load

/usr/share/gdb/auto-load already exists on my Ubuntu installation. It's the gdb version 7.11.1.

After that gdb is showing container contents. Thanks Mark.

Michael Troger
  • 3,336
  • 2
  • 25
  • 41
2

The gdb native to Ubuntu 16.04 should be able to pretty print a std::map; the python code which handles that is class StdMapPrinter in the following file from the libstdc++6 package:

/usr/share/gcc-5/python/libstdcxx/v6/printers.py

If your goal is to accomplish this with binaries you build from source, it might still be useful to get this working with the native Ubuntu packages first, as a basis for comparison. The likely reason pretty printers are not working in a gdb you built yourself is that libstdcxx/v6/printers.py (libstdcxx.v6.printers in the python namespace) is not where gdb expects to find it.

Also, the gdb native to Ubuntu 16.04 is linked against python3, not python2; one way to see that is in the output of

ldd /usr/bin/gdb

It appears that the libstdcxx.v6.printers native to Ubuntu 16.04 is intended to work with either python2 or python3.

If your native gdb binary is not already installed, you can use this:

apt install gdb
Eirik Fuller
  • 1,454
  • 11
  • 9