Difference between revisions of "Pretty Printers"

From Code::Blocks
Line 15: Line 15:
 
:Windows users with MinGW should already have this file in /MinGW/share/gcc-4.7.2/python/libstdcxx/v6.
 
: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 [http://gcc.gnu.org/viewcvs/trunk/libstdc%2B%2B-v3/python/libstdcxx/v6/ here]. Store in /home/username/gdb_printers.
 
:Linux users can download printers.py [http://gcc.gnu.org/viewcvs/trunk/libstdc%2B%2B-v3/python/libstdcxx/v6/ here]. Store in /home/username/gdb_printers.
*Create a GDB [http://sourceware.org/gdb/onlinedocs/gdb/Command-Files.html Command File] to enable the printer. Store in c:\mingw\bin\pp.gdb (windows) or /home/username/gdb_printers (linux). Below is a sample command file. Replace the path c:/MinGW/share... with your path to printers.py.
+
*Create a GDB [http://sourceware.org/gdb/onlinedocs/gdb/Command-Files.html Command File] to enable the printer. Store in <tt><span style="font-size: 10pt">c:\mingw\bin\pp.gdb</span></tt> (windows) or <tt><span style="font-size: 10pt">/home/username/gdb_printers/pp.gdb</span></tt> (linux). Below is a sample command file. Replace the path c:/MinGW/share... with your path to printers.py.
 
<blockquote><span style="font-size: 10pt"><pre>
 
<blockquote><span style="font-size: 10pt"><pre>
 
python
 
python

Revision as of 01:46, 27 October 2012

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


Popup example

Test with GDB

  • Ensure GDB is python-enabled. For Linux (tested with recent Ubuntu), it is included by default with GCC/GDB. 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. Store in /home/username/gdb_printers.
  • Create a GDB Command File to enable the printer. Store in c:\mingw\bin\pp.gdb (windows) or /home/username/gdb_printers/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
  1. Set a breakpoint in a program and debug
  2. 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
  1. 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:

  1. 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 may prevent entering anything in the Debugger initialization commands field. Until this is fixed, a workaround is to edit ./home/username/.codeblocks/default.conf and change the INIT_COMMANDS value to something like:
    <![CDATA[source /home/user/gdb_pprinters/pp.gdb]]>

  1. 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?