Debugging C ++ programs on GNU / Linux

It just so happened that for a long time I spend a lot of time with the operating systems of the GNU / Linux family. The main type of my activity is software development in C ++.

So, the main problem when using the debugger is the display of complex containers, for example, stl containers.

The solution I propose is relevant for gdb. This debugger supports scripts written in python, and the mechanisms for displaying complex objects are called pretty printers. Those. In order for the debugger to display everything to us correctly, you need to tell it where the scripts with these pretty printers are located. To specify additional commands to the debugger, a .gdbinit file is required.

So, I’ll try to arrange everything, both the instruction and reading more conveniently, and I myself will not forget.

1. Install gcc, g ++, gdb, libstdc ++ - dbg , the latter is very important, because in fresh distributions with debugging symbols, scripts with pretty printers are installed, for example, in Ubuntu 14.04 the /usr/share/gcc-4.8/python/libstdcxx directory appears.
2. Create a .gdbinit file in the home directory with the following contents:

# .gdbinit : GDB Config file
# add python pretty printers for STL
import sys
sys.path.insert(0, '/usr/share/gcc-4.8/python')
from libstdcxx.v6.printers import register_libstdcxx_printers
register_libstdcxx_printers (None)
set auto-load local-gdbinit on
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

Everything seems to be fine, but there is a problem, in the latest versions of GNU / Linux, gdb is built with support for python version 3. *, and the default is python version 2. *. To fix this problem, i.e. to make scripts that support both versions of python there is a patch, you can find it on the open spaces of the network, you can take it here: _https: // Subject to change is /usr/share/gcc-4.8/python/libstdcxx/v6/

And again, everything seems to be fine, but again there is a problem, on some distributions the very scripts with pretty printers are not installed. To do this, there is the svn repository _svn: // Here is a link with an additional description: _http: // If this is your case, then after unloading the repository, do not forget to change the path to downloaded scripts to the .gdbinit file.

3. When assembling a project with debugging, be sure to specify the _GLIBCXX_DEBUG flag. This is necessary so that containers such as stringstream are displayed correctly.

In conclusion, I want to say that “dancing” with pretty printers (the second point) is not needed if you use QtCreator as an IDE, as this IDE uses its scripts to interact with gdb.

Also popular now: