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

45,282

Solution 1

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

Solution 2

I know this does not answer the original question, but I thought it might be useful for those who debug in Eclipse on Windows / MinGW. The procedure for Windows is similar:

1) Get Python 2.7.x from http://python.org/download/

2) Create .gdbinit somewhere containing something like this:

python
import sys
sys.path.insert(0, 'C:/MinGW/share/gcc-4.6.1/python')
from libstdcxx.v6.printers import register_libstdcxx_printers
register_libstdcxx_printers (None)
end

3) Configure Eclipse CDT to use C:\MinGW\bin\gdb-python27.exe as the debugger and your .gdbinit as the config file.

4) Re-create your debug launch session (delete the old one and create a new one from scratch).

Solution 3

I would like to expand on the Windows 7 response because some key steps are left out:

This is for MinGW users with Eclipse CDT

0) If you don't have python GDB, open a shell/command and use MinGW-get.exe to 'install' Python-enabled GDB e.g.

   MinGw-get.exe install gdb-python

1a) Get Python 2.7.x from http://python.org/download/ and install

1b) Make sure PYTHONPATH and PYTHONHOME are set in your environment:

 PYTHONPATH should be C:\Python27\Lib   (or similar)
 PYTHONHOME should be C:\Python27

1c) Add PYTHONHOME to your PATH

 %PYTHONHOME%;...

2a) Open a text enter, enter the following statements. Notice the 3rd line is pointing to where the python scripts are located. See notes below about this!

python
import sys
sys.path.insert(0, 'C:/MinGW/share/gcc-4.6.1/python')         
from libstdcxx.v6.printers import register_libstdcxx_printers
register_libstdcxx_printers (None)
end

2b) Save as '.gdbinit' NOTE: Windows explorer will not let you name a file that starts with with a period from explorer. Most text edits (including Notepad) will let you. GDB init files are like 'scripts' of GDB commands that GBD will execute upon loading.

2c) The '.gdbinit' file needs to be in the working directory of GDB (most likely this is your projects root directory but your IDE can tell you.

3) Open your Eclipse (or other IDE) Preferences dialog. Go to the C++ Debugger sub-menu.

4) Configure Eclipse to use C:\MinGW\bin\gdb-python27.exe as the debugger and your .gdbinit as the config file.

5a) Re-create all your debug launch configurations (delete the old one and create a new one from scratch).

--OR--

5b) Edit each debug configuration and point it to the new gdb-python.exe AND point it to the.

If you run into issues:

--Don't forget to change the location to the python directory in the above python code! This directory is created by MinGW, so don't go looking to download the pretty printers, MinGW did it for you in step zero. Just goto your MinGW install director, the share folder, the GCC folder (has version number) and you will find python folder. This location is what should be in python script loaded by GDB.

--Also, the .gdbinit is a PITA, make sure its named correctly and in the working folder of GDB which isn't necessarily where gdb-python.exe is located! Look at your GDB output when loading GDB to see if a) 'python-enabled' appears during load and that the statements in the .gdbinit are appearing.

--Finally, I had alot of issues with the system variables. If python gives you 'ImportError' then most likely you have not set PYTHONPATH or PYTHONHOME.

--The directory with 'gdb-python27' (e.g. C:\MinGW\bin') should also be on your path and if it is, it makes setting up eclipse a bit nicer because you don't need to put in absolute paths. But still, sometimes the .gbdinit needs an absoulte path. if it works you'll see output from gbd (console->gdb traces) like this on startup of debugger:

835,059 4^done
835,059 (gdb) 
835,059 5-enable-pretty-printing
835,069 5^done
....
835,129 12^done
835,129 (gdb) 
835,129 13source C:\MinGW\bin\.gdbinit
835,139 &"source C:\\MinGW\\bin\\.gdbinit\n"
835,142 13^done
835,142 (gdb) 

Solution 4

If you follow the accepted answer and UPDATE2 and gdb receives an error like this:

Traceback (most recent call last):
  File "<string>", line 3, in <module>
  File "/usr/share/gcc-4.8/python/libstdcxx/v6/printers.py", line 54
    raise ValueError, "Cannot find type %s::%s" % (str(orig), name)
                ^
SyntaxError: invalid syntax
/home/[you]/.gdbinit:6: Error in sourced command file:
Error while executing Python code.

It is because your version of gdb is using Python 3 (confirm with this answer)

A workaround for this problem is explained here

Alternatively, follow the instructions to get the source from svn://gcc.gnu.org, which is Python 3 compatible

Solution 5

to display STL objects in Eclipse CDT, I do this and it works fine:

http://www.yolinux.com/TUTORIALS/GDB-Commands.html#STLDEREF

(option number 2 in the link that you sent)

Hope this helps

Share:
45,282
AlejandroVK
Author by

AlejandroVK

I'm a normal guy with a passion for technology and solving real life problems. I've worked for a long time in the R&amp;D industry, participating in several IoT European research projects until I decided to create my own start-up with my colleague. Then I quickly moved to Web and Mobile development, using technologies like Python, Django-Flask, Javascript (Angular-Ember-React), MongoDB, PostgreSQL, Chef, Vagrant, Docker, Ansible, etc. In one word, I'm flexible, can switch back and front to devops as needed, and I learn fast.

Updated on July 09, 2022

Comments

  • AlejandroVK
    AlejandroVK almost 2 years

    I'm trying to add pretty printing for STL objects in eclipse cdt. I tried to follow the steps described here:

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

    I checked out the python folder, but I can't seem to get this done...

    I created a gdbinit and selected for my debug configuration, but whenever I try to start debugging I get the following error:

    Error while executing Python code.
    !STACK 0
    java.lang.Exception: /home/lizardking/workspace/eu.sofia.kpi.cpp.x86.testapp/.gdbinit:6: Error in sourced command file:
    Error while executing Python code.
            at org.eclipse.cdt.dsf.mi.service.command.AbstractMIControl$RxThread.processMIOutput(AbstractMIControl.java:824)
            at org.eclipse.cdt.dsf.mi.service.command.AbstractMIControl$RxThread.run(AbstractMIControl.java:662)
    

    If I try to execute the contents of gdbinit in a python shell, I get this error:

    Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39) 
    [GCC 4.4.5] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    import sys
    sys.path.insert(0, '/home/Documents/python')
    from libstdcxx.v6.printers import register_libstdcxx_printers
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ImportError: No module named libstdcxx.v6.printers
    

    It seems that I don't have such module...I don't have a clue about Python so I don't even know what a "module" is in Python....

    Can somebody help me with this? It is very important for me to be able to see real debug information, or useful to put it that way. Or someway I can debug even from console and get nice output from gdb, cuz if I go print a string for instance I get useless output....

    Regards, Alex

  • AlejandroVK
    AlejandroVK over 13 years
    Hey Bob, I used that script but I still get all the "useless" output, for instace, if I want to see the contents of a string, this is what I get, even typing print in gdb console: $1 = { static npos = <optimized out>, _M_dataplus = { <std::allocator<char>> = { <__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, members of std::basic_string<char, std::char_traits<char>, std::allocator<char> >::_Alloc_hider: _M_p = 0x8066574 "<SSAP_message>" } Ok, I tried more and I can see it works if I go with pstirng commnad it prints a nice output info...is t
  • AlejandroVK
    AlejandroVK over 13 years
    I have used the option as Bob suggested and although it works fine, I hate having to switch to gdb thread every time I need to inspect a stl container...it would be great to integrate the script inspecting abilities with mouse hovering and the variable inspection panel... Has anyone achieved this?
  • AlejandroVK
    AlejandroVK about 13 years
    Hey Riga, thanks for the very detailed explanation. Pretty printing works from the shell. I downloaded both links for Eclipse but I can't seem to make this work. I have specified my $HOME/.gdbinit file in Windows-Preferences-Debug-GDB but it was not printing strings, maps, etc nicely, so I also specified this file in the Debug Configuration for a project. Now this seems to be printing "prettier" but at some point, like declaration of a map, debugging stops and it won't continue passed that line...really strange, no error popping out or nothing. Has this happened to you? Thanks!
  • Riga
    Riga about 13 years
    @Alejandro I work with no problems with this configuration and have no ideas how to reproduce your problem to help you. You can try to report the error here bugs.eclipse.org/bugs/show_bug.cgi?id=302121
  • AlejandroVK
    AlejandroVK about 13 years
    Thanks Riga, I created another test project and it works fine, although I have to specify the .gdbinit per project, otherwise I wont get pretty printing. Besides, where should I execute those set commands? shell? Thanks again for the very nice explanation
  • Riga
    Riga about 13 years
    @Alejandro you can append additional commands for gdb to the end of .gdbinit file. If my answer works you can accept it by clicking accept sign at the left at the answer.
  • AlejandroVK
    AlejandroVK about 13 years
    Hi again Rigga! You're a star! this definitly works! I'm sorry I can't vote your answer, I need to have 15 rep in order to do so...:(, but I hope someone reads this and knows your method truly works! Thanks again
  • Riga
    Riga about 13 years
    @Alejandro thank you, you cannot vote, but you can accept an answer to your question instead. Just click on the gray V sign that is placed under voting arrows at the left of my question. Good luck!
  • arberg
    arberg over 11 years
    Regarding getting it to work for old projects, see point 4) in rustyx answer below.
  • arberg
    arberg over 11 years
    Where did you get C:\MinGW\bin\gdb-python27.exe? I tried mingw.org, but its gdb release does not contain python. I tried sourceforge.net/projects/mingwbuilds (x32-4.7.2-release-win32-sjlj-rev0 and x64-4.7.2-release-posix-sjlj-rev0) but they also does not contain python gdb it appears.
  • Halil Kaskavalci
    Halil Kaskavalci over 11 years
    mingw-get install gdb-python would do its job.
  • Andre Miras
    Andre Miras over 10 years
    FWIW, ubuntu 12.04 has libstdc++6-4.6-dbg that installs /usr/share/gcc-4.6/python/libstdcxx/ python module for you.
  • truthseeker
    truthseeker almost 10 years
    gdb-python27.exe crashes when stepping into method containing STL variables.
  • stu
    stu about 8 years
    I've recently had problems with this where it causes gdb to hang while debugging in cdt. I got a stack trace of the gdb that's spinning a cpu and the problem is buried in the python pretty printer. Any idea who owns it so I can open a bug? I have this sourceware.org/bugzilla/show_bug.cgi?id=19894 but I'm not sure it's the correct place.
  • einpoklum
    einpoklum about 8 years
    Doesn't work for me on Fedora 23 (which already has this preconfigured I think, that is, .gdbinit is already full of pretty-printing related stuff).