Difference between revisions of "Pretty Printers"

From Code::Blocks
Line 3: Line 3:
 
[[File:Pprint1.png]]<br>
 
[[File:Pprint1.png]]<br>
 
UNDER CONSTRUCTION
 
UNDER CONSTRUCTION
==Step 1 - Get it working with GDB==
+
==Step 1 - Test with GDB==
  
#Install a python-enabled GDB. If you're on Windows, you can install [http://sourceforge.net/projects/mingwbuilds/ MinGW-Builds] over MinGW (should probably backup MinGW first). This updates GCC to 4.7.2 and includes a Python enabled GDB.
+
*Install a python-enabled GDB. If you're on Windows, you can install [http://sourceforge.net/projects/mingwbuilds/ MinGW-Builds] over MinGW (consider backing up MinGW first). This updates GCC to 4.7.2 and includes a Python enabled GDB.
#Create a GDB [http://sourceware.org/gdb/onlinedocs/gdb/Command-Files Command File] to enable the printer. Store in c:\mingw\bin\pp.gdb (or wherever you want). Here is a sample command file. Replace the path with your path to printers.py. NOTE: A Python STL printer.py is included with MinGW and MinGW-Builds, so there is no need to download one. It only needs to be turned on, which is the purpose of the command file
+
*Create a GDB [http://sourceware.org/gdb/onlinedocs/gdb/Command-Files Command File] to enable the printer. Store in c:\mingw\bin\pp.gdb (or wherever you want). Here is a sample command file. Replace the path with your path to printers.py. NOTE: A Python STL printer.py is included with MinGW and MinGW-Builds, so there is no need to download one. It only needs to be turned on, which is the purpose of the command file
<blockquote>
+
<blockquote><pre>
<pre>
 
 
python
 
python
 
import os, sys
 
import os, sys
Line 17: Line 16:
 
register_libstdcxx_printers (None)
 
register_libstdcxx_printers (None)
 
end
 
end
</pre>
+
</pre></blockquote>
</blockquote>
+
*Test
[list][li]Test in GDB
+
#Set a breakpoint in a program and debug
[list type=decimal]
+
#Run GDB command file (can use Codeblocks debugger tab command, or GDB from console) (substitute your path if necessary)
[li]Set a breakpoint in a program and debug[/li]
+
<blockquote><pre>
[li]Run GDB command file  (can use Codeblocks debugger tab command, or GDB from console) (substitute your path if necessary)[/li]
 
[/list][code]
 
 
(gdb) source c:\MinGW\bin\pp.gdb
 
(gdb) source c:\MinGW\bin\pp.gdb
[/code]
+
</blockquote></pre>
 
[li]Test the printer - example:[/li]
 
[li]Test the printer - example:[/li]
 
[code](gdb) print words2
 
[code](gdb) print words2
Line 40: Line 37:
 
1) Edit pathto-Codeblocks\share\CodeBlocks\scripts\gdb_types.script
 
1) Edit pathto-Codeblocks\share\CodeBlocks\scripts\gdb_types.script
 
2) Add comments as follows:[/li][/list]
 
2) Add comments as follows:[/li][/list]
[code]
+
<pre>
 
     /* STL String
 
     /* STL String
 
     driver.RegisterType(
 
     driver.RegisterType(
Line 56: Line 53:
 
         _T("Parse_StlVector")
 
         _T("Parse_StlVector")
 
     ); */
 
     ); */
[/code][list]
+
</pre>
[/list]
 
 
[u]Other Info[/u]
 
[u]Other Info[/u]
 
Links:
 
Links:

Revision as of 16:22, 25 October 2012

GDB Pretty Printers for STL output nicely formatted variables, even for vectors and maps. This works in GDB, and if enabled, in the hover pop-up and watch window in Code::Blocks.


Pprint1.png
UNDER CONSTRUCTION

Step 1 - Test with GDB

  • Install a python-enabled GDB. If you're on Windows, you can install MinGW-Builds over MinGW (consider backing up MinGW first). This updates GCC to 4.7.2 and includes a Python enabled GDB.
  • Create a GDB Command File to enable the printer. Store in c:\mingw\bin\pp.gdb (or wherever you want). Here is a sample command file. Replace the path with your path to printers.py. NOTE: A Python STL printer.py is included with MinGW and MinGW-Builds, so there is no need to download one. It only needs to be turned on, which is the purpose of the command file
python
import os, sys
lib_path = os.path.abspath('c:/MinGW/share/gcc-4.7.0/python/libstdcxx/v6')
sys.path.append(lib_path)
#print 'path is [%s]' % ', '.join(map(str, sys.path))
from printers import register_libstdcxx_printers
register_libstdcxx_printers (None)
end
  • Test
  1. Set a breakpoint in a program and debug
  2. Run GDB command file (can use Codeblocks debugger tab command, or GDB from console) (substitute your path if necessary)
(gdb) source c:\MinGW\bin\pp.gdb
</blockquote>

[li]Test the printer - example:[/li] [code](gdb) print words2 $1 = std::vector of length 3, capacity 4 = {"one", "two", "three"}[/code] [/list] [hr] [u]Step 2 - Add to Codeblocks[/u] Once the printer works in GDB, there are two steps to activate in Codeblocks: [list] [li]Set debugger initialization command settings->debugger->default->debugger initialization commands[/li][/list] [code]source $(TARGET_COMPILER_DIR)bin\pp.gdb[/code] [list][li]Comment out the Codeblocks gdb handler 1) Edit pathto-Codeblocks\share\CodeBlocks\scripts\gdb_types.script 2) Add comments as follows:[/li][/list]

    /* STL String
    driver.RegisterType(
        _T("STL String"),
        _T("[^[:alnum:]_]*string[^[:alnum:]_]*"),
        _T("Evaluate_StlString"),
        _T("Parse_StlString")
    );*/

    /* STL Vector
    driver.RegisterType(
        _T("STL Vector"),
        _T("[^[:alnum:]_]*vector<.*"),
        _T("Evaluate_StlVector"),
        _T("Parse_StlVector")
    ); */

[u]Other Info[/u] Links: http://sourceware.org/gdb/onlinedocs/gdb/Python-API.html http://sourceware.org/gdb/onlinedocs/gdb/Pretty-Printing.html

[u]To Do[/u] 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?

Mod: Please move if this is the wrong forum.