How to Pretty-Print Stl Containers in Gdb

How does gdb print stl containers

How does GDB pretty print this container?

By using pretty-printers.

The pretty-printers for libstdc++ std::vector, std::unordred_map etc. are part of and shipped with libstdc++.

I see the pretty printers are located in the stl objfile

No, they are not. A filename pointing to them is in the object file, but not the pretty-printers themselves.

C++ gdb pretty printing in Ubuntu 18.04 visual studio code

I found a solution as posted here. The gdb was not able to find the location where the python printers.py was located. The file was located under /usr/share/gcc/python/libstdcxx/v6/printers.py.

What I needed to do is create a .gdbinit file on my home directory including the following lines of code

python
import sys
sys.path.insert(0, '/usr/share/gcc/python')
from libstdcxx.v6.printers import register_libstdcxx_printers
register_libstdcxx_printers (None)
end

Next source the file with source .gdbinit and then try again the info pretty-print. All the alternate options are now available. Subsequently, gdb debugging and vscode was able to show the contents of the C++ STL containers.

How to enable gdb pretty printing for C++ STL objects in Eclipse CDT?

This is the solution that works for me.

Download ( http://www.gnu.org/software/gdb/download/) and install latest gdb (i.e. with --prefix $HOME). It supports python scripting.

Get python pretty printers by executing

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

in a directory of your choice (i.e. $(HOME)/distribs/gdb_printers). You will get 'python' subdirectory in the checkout directory.

Put this in your $(HOME)/.gdbinit file with proper path to pretty printers:

python
import sys
sys.path.insert(0, '/home/YOUR_NAME_HERE/distribs/gdb_printers/python')
from libstdcxx.v6.printers import register_libstdcxx_printers
register_libstdcxx_printers (None)
end

This makes pretty printing usable via command-line interface of gdb ( >(gdb) p my_std_string).

Next explains usage while debugging in Eclipse.

Download ( http://download.eclipse.org/eclipse/downloads/) latest Stream Stable Build or Release of Eclipse (>=3.7 version).

Download ( http://download.eclipse.org/tools/cdt/builds/8.0.0/index.html for Eclipse Indigo or http://www.eclipse.org/cdt/downloads.php for Eclipse Juno) latest Eclipse C/C++ Development Tooling (Eclipse CDT).

Run Eclipse and chose workspace directory where your options will be stored (i.e. $HOME/projects). Click Help->Install New Software... Click Add...->Archive... and choose the CDT build that you've just downloaded. Then you must choose components to install: click CDT Main Features -> C/C++ Development Tools (and possibly other components of your choice). Then proceed with installation and restart Eclipse.

Specify proper location of gdb and .gdbinit in Eclipse and make sure the Pretty Printing option is enabled:

Window -> preferences -> C/C++ -> Debug -> GDB

Now you can see STL containers pretty-printed in Variables view while debugging in Eclipse.

Other commands can be used to make gdb output more decent:

set print pretty on
set print object on
set print static-members on
set print vtbl on
set print demangle on
set demangle-style gnu-v3
set print sevenbit-strings off

UPDATE: Regarding getting it to work for old projects, see point 4) in rustyx answer below.

UPDATE2: ubuntu 12.04 has libstdc++6-4.6-dbg that installs /usr/share/gcc-4.6/python/libstdcxx/ python module for you

gdb pretty printing is not working

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.

GDB: Pretty-Print class containing STL container

After some trying, I've found a way that comes very close. I'm basically using the default StdSetPrinter provided with the stdlib, but I'm not using it for printing, just for iterating the set. My code looks like this now:

from libstdcxx.v6.printers import StdSetPrinter

class FooPrinter(object):
def __init__(self, val):
self.val = val

def to_string(self):
return "X: " + str(self.val['x'])

class FooContainerPrinter(object):
def __init__(self, val):
self.val = val

def to_string(self):
return "My Foo Container"

def children(self):
pp = StdSetPrinter("dummy", self.val['content'])
return pp.children()

Now, the default pretty printing magic still adds some boilerplate (basically it outputs "My Foo Container = { … ⟨ pretty-print of the contents ⟩ …}") but that is fine with me. I think it would even be able to not define an own children(), but rather use the pp.children() inside to_string() and thus have full control over the output string.

It has the drawback that the path where libstdc++ puts its default pretty printers needs to be in the PYTHONPATH.

Getting GDB pretty printing for C++ stl containers to work on Eclipse

To get you started, you can compile GDB in a separate directory, and run it from there.

Grab the sources for the version you want:
http://sources.redhat.com/gdb/

Run ./configure with the --with-python, and then make, but don't install it over your system copy.

At that point, you should be able to invoke gdb where it has been built with ./builddir/gdb, rather than the one in your path. (This is where you should point eclipse debugging to, if you want to invoke it from there)

Print elements of C++ string vector nicely in GDB

Is there any way to view the elements nicely without displaying some part of the STL containers?

You either have a very old GDB, or some non-standard setup.

Here is what it looks like on a Fedora-34 system with default GDB installation:

(gdb) list
1 #include <string>
2 #include <vector>
3
4 int main()
5 {
6 std::vector<std::string> v;
7 v.push_back("abc");
8 v.push_back("abcdef");
9 v.push_back("ghi");
10 }
(gdb) b 9
Breakpoint 1 at 0x401378: file t.cc, line 9.
(gdb) run
Starting program: /tmp/a.out
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".

Breakpoint 1, main () at t.cc:9
9 v.push_back("ghi");
(gdb) p v
$1 = std::vector of length 2, capacity 2 = {"abc", "abcdef"}
(gdb) n
10 }
(gdb) p v
$2 = std::vector of length 3, capacity 4 = {"abc", "abcdef", "ghi"}
(gdb) q


Related Topics



Leave a reply



Submit