Menu

#453 Display STL std::string like char* in debugger

Undefined
invalid
Feature_Request
2016-12-22
2016-12-22
W Sforge
No

Would be nice if codeblock would treat STLstd::string from mingw/gdb as a special case and
map the char* buried inside the STL string data structure as the first think you see when adding std::string to the watch window.

This is one of the number on most annoying problems of debugging C++ code in codeblocks or just about any debugger with gdb... would be nice to have a special bandaid built into codeblock just for this one case...

Discussion

  • Teodor Petrov

    Teodor Petrov - 2016-12-22
    • status: open --> invalid
    • assigned_to: Teodor Petrov
     
  • Teodor Petrov

    Teodor Petrov - 2016-12-22

    This is a job of gdb to do it. See how to install and enable pretty printers for gdb.

    Closing as invlid.

     
  • W Sforge

    W Sforge - 2016-12-22

    Thanks for the tip.

     
  • W Sforge

    W Sforge - 2016-12-22

    just googled youtube "Qt Creator and GDB: Pretty Printer And Container Customization Tutorial"

    If you watch the video you will see that QtCreate has this feature built into the IDE...

     
  • W Sforge

    W Sforge - 2016-12-22

    CodeLite has it integrated with IDE also:
    http://codelite.org/LiteEditor/GdbPrettyPrinting

    GDB Pretty Printing
    Preface

    GDB pretty printing is a gdb feature that allows gdb to display complex objects (and other containers) in a more friendly way.

    For example, when gdb pretty printing is disabled, viewing an STL map is pretty much useless. Consider the following code:

    typedef std::map<std::string, int=""> StringToIntMap_t;
    ...
    StringToIntMap_t mymap;
    mymap.insert( std::make_pair("String One", 1) );
    mymap.insert( std::make_pair("String Two", 2) );
    mymap.insert( std::make_pair("String Three", 3) );

    Without pretty printing enabled, viewing mymap in the Locals view will give the following:

    However, with Pretty Printing enabled, it will look like this:
    Setting up pretty printing

    Before you can enable this feature within CodeLite, you will need to install the following:

    GDB with Python enabled. For Windows users, I recommend downloading the one from here (just extract it somewhere)
    Python 2.7.x installed on your system.
    

    Make sure to point CodeLite to the correct gdb. You can do this from the main menu Settings -> GDB Settings -> GNU gdb debugger -> General
    and select the new gdb in the Debugger path field.

    Once everything is set up properly, go to CodeLite's menu bar: Settings -> GDB Settings -> GNU gdb debugger -> General and enable the option Enable GDB pretty printing.

    Next, switch to Start up commands and make sure you have the following startup commands:

    python
    import sys
    sys.path.insert(0, 'PATH_TO_CODELITE_SETTINGS_FOLDER/gdb_printers')

    from libstdcxx.v6.printers import register_libstdcxx_printers
    register_libstdcxx_printers (None)

    from qt4 import register_qt4_printers
    register_qt4_printers (None)

    from wx import register_wx_printers
    register_wx_printers (None)
    end

    Note: make sure to replace PATH_TO_CODELITE_SETTINGS_FOLDER with the correct path to your gdb_printers (CodeLite creates this directory when it is launched for the first time).
    Under Linux, you should replace it with:

    ~/.codelite/gdb_printers

    and under Windows it should point to:

    %APPDATA%\CodeLite\gdb_printers

    Replace the placeholder PATH_TO_CODELITE_SETTINGS_FOLDER with the absolute path. Do not use environment variables etc

    This is how the Startup Commands looks on my Windows box:

     
  • W Sforge

    W Sforge - 2016-12-22

    Eclipse has support as well for pretty printers built in:

    How can I inspect the contents of STL containers?

    CDT debug now supports full pretty-printing of STL structures using GDB 7.0 or later. This means that complex structures such as Maps, Lists and Vectors, will be shown in a user-friendly fashion. This does require proper setup of GDB as described below.

    Without pretty-printing:

    NoPrettyPrint.png

    With pretty-printing:

    FullPrettyPrint.png

    Configuring GDB for pretty-printing:

    You will need to have python installed on your machine

    If you want to pretty-print STL structures, you will need the Python pretty-printers for STL. Check-out the latest Python libstdc++ printers to a place on your machine. (Note that you can create your own pretty-printers for any complex-structure). In a local directory, do:

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

    The following is a workaround for a gdb bug (http://sourceware.org/bugzilla/show_bug.cgi?id=12555) until it is finally fixed there: In the printers.py, locate StdStringPrinter#to_string. Right after the calculation of len, add the following lines (use whatever limit you feel is appropriate):

       if len > 100:
          len = 100
    

    You will need to create a gdbinit file to tell GDB where the pretty-printers are. Create a gdbinit file with the following 6 lines. The path needs to match where the python module above was checked-out. So if checked out to: /home/marc/gdb_printers/, the path would be as written in the example:

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

    You will need GDB 7.0 or later. That latest version of GDB is recommended because it has bug fixes for the pretty-printing.

    In your CDT launch, make sure it says "Using GDB (DSF) ..."

    In your CDT launch, make sure you use the right GDB and the right gdbinit file. You need to tell eclipse where .gdbinit is located - Window -> preferences -> C/C++ -> Debug -> GDB

     

    Last edit: W Sforge 2016-12-22
  • W Sforge

    W Sforge - 2016-12-22

    OK. I FOUND IT.

    Codeblock pretty printers for C++ STL

    GDB Pretty Printers for STL display nicely formatted variables in the hover pop-up and watch window, for all STL containers (vectors, maps, etc).

    Ensure GDB is python-enabled. For Linux (tested with recent Ubuntu), it is enabled by default. For Windows, MinGW's GDB is not python enabled. One option is to install MinGW-Builds over MinGW (consider backing up MinGW first). This updates GCC to 4.7.2 and includes a Python enabled GDB.

    To test, launch GDB from console:

    (gdb) python print sys.version

    If python is enabled, the version will be printed (probably 2.7.x), otherwise, a message will indicate python scripting is not supported.

    Download printers.py (if necessary)

    Windows users with MinGW should already have this file in /MinGW/share/gcc-4.7.2/python/libstdcxx/v6.
    Linux users can download printers.py here. Save as /home/username/gdb_printers/printers.py.

    Create a GDB Command File to enable the printer. Store in c:\mingw\bin\pp.gdb (windows) or /home/username/gdb_printe /pp.gdb (linux). Below is a sample command file. Replace the path c:/MinGW/share... with your path to printers.py.

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

    Test

    Set a breakpoint in a program and debug
    Run the command file from GDB (can use Codeblocks->debugger tab->command, or in GDB from the console) (substitute your path if necessary)

    (gdb) source c:\MinGW\bin\pp.gdb

    Test the printer - example:

    (gdb) print words2
    $1 = std::vector of length 3, capacity 4 = {"one", "two", "three"}

    Add to Codeblocks

    Once the command file is working correctly, there are two steps to activate in Codeblocks:

    Set debugger initialization command (substitute your path as necessary):

    codeblocks->Settings->Debugger->Default->Debugger initialization commands

    source c:\MinGW\bin\pp.gdb

    NOTE: A bug in the Linux version of Codeblocks may prevent entering nything in the Debugger Initialization Commands field. A work-around is to open a CBP project file via a file manager, which in turn launches Codeblocks and seems to resolve the issue.

    Disable Codeblocks handling of watch values:

    Codeblocks->Settings->Debugger->Default->Enable Watch Scripts = Unchecked

    Other Info

    Links:

    GDB Python API

    GDB Pretty Printing
    To Do

    The third column in the Codeblocks popup and watch window displays a long unformatted string. Codeblocks is calling the GDB whatis command. Can this command be Pretty-Printed?

    http://wiki.codeblocks.org/index.php/Pretty_Printers

     

    Last edit: W Sforge 2016-12-22

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.