Difference between revisions of "Pretty Printers"

From Code::Blocks
m (add a new stub for pretty printer for msys2)
 
(63 intermediate revisions by 4 users not shown)
Line 1: Line 1:
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.
+
[[Category: User Documentation]]
 +
GDB Pretty Printers for STL display nicely formatted variables in the hover pop-up and watch window, for all STL containers (vectors, maps, etc).
 
----
 
----
[[File:Pprint1.png]]<br>
+
[[File:Pprint1.png|border|Popup example]]<br>
UNDER CONSTRUCTION
+
==Test with GDB==
==Step 1 - Get it working with GDB==
 
  
#Install a python-enabled GDB. If you're on Windows, you can install [url=http://sourceforge.net/projects/mingwbuilds/]MinGW-Builds[/url] over MinGW (should probably backup MinGW first). This updates GCC to 4.7.2 and includes a Python enabled GDB.
+
*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 [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 [url=http://sourceware.org/gdb/onlinedocs/gdb/Command-Files.html]Command file[/url] to enable the printer. Store in c:\mingw\bin\pp.gdb (or wherever you want). Here is my 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
+
<ol>
<blockquote>
+
To test, launch GDB from console:
<pre>
+
<span style="font-size: 10pt"><pre>
 +
(gdb) python print sys.version
 +
</pre></span>
 +
If python is enabled, the version will be printed (probably 2.7.x), otherwise, a message will indicate python scripting is not supported.
 +
</ol>
 +
*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 [http://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python/libstdcxx/v6/printers.py here]. Save as <tt><span style="font-size: 10pt">/home/username/gdb_printers/printers.py</span></tt>.
 +
*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>
 
python
 
python
import os, sys
+
import sys
lib_path = os.path.abspath('c:/MinGW/share/gcc-4.7.0/python/libstdcxx/v6')
+
sys.path.insert(0, 'c:/MinGW/share/gcc-4.7.2/python/libstdcxx/v6')
sys.path.append(lib_path)
 
#print 'path is [%s]' % ', '.join(map(str, sys.path))
 
 
from printers import register_libstdcxx_printers
 
from printers import register_libstdcxx_printers
 
register_libstdcxx_printers (None)
 
register_libstdcxx_printers (None)
 
end
 
end
</pre>
+
</pre></span></blockquote>
</blockquote>
+
*Test
[list][li]Test in GDB
+
#Set a breakpoint in a program and debug
[list type=decimal]
+
#Run the command file from GDB (can use Codeblocks->debugger tab->command, or in GDB from the console) (substitute your path if necessary)
[li]Set a breakpoint in a program and debug[/li]
+
<ol><span style="font-size: 10pt"><pre>(gdb) source c:\MinGW\bin\pp.gdb</pre></span></ol>
[li]Run GDB command file  (can use Codeblocks debugger tab command, or GDB from console) (substitute your path if necessary)[/li]
+
<ol start="3">
[/list][code]
+
<li>Test the printer - example:</li>
(gdb) source c:\MinGW\bin\pp.gdb
+
</ol>
[/code]
+
<ol><span style="font-size: 10pt"><pre>(gdb) print words2
[li]Test the printer - example:[/li]
+
$1 = std::vector of length 3, capacity 4 = {"one", "two", "three"}
[code](gdb) print words2
+
</pre></span></ol>
$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]
 
[code]
 
   /* STL String
 
   driver.RegisterType(
 
       _T("STL String"),
 
       _T("[^[:alnum:]_]*string[^[:alnum:]_]*"),
 
       _T("Evaluate_StlString"),
 
       _T("Parse_StlString")
 
   );*/
 
  
   /* STL Vector
+
==Add to Codeblocks==
   driver.RegisterType(
+
Once the command file is working correctly, there are two steps to activate in Codeblocks:
       _T("STL Vector"),
+
#Set debugger initialization command (substitute your path as necessary):<br/><span style="font-size: 10pt"><tt>Codeblocks->Settings->Debugger->Default->Debugger initialization commands</tt></span>
       _T("[^[:alnum:]_]*vector<.*"),
+
<ol><span style="font-size: 10pt"><pre>source c:\MinGW\bin\pp.gdb</pre></span>
       _T("Evaluate_StlVector"),
+
NOTE: A bug in the Linux version of Codeblocks may prevent entering anything 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.
       _T("Parse_StlVector")
+
</ol>
   ); */
+
<ol start="2">
[/code][list]
+
<li>Disable Codeblocks handling of watch values (needed only for versions older than 17.12):</li>
[/list]
+
</ol>
[u]Other Info[/u]
+
<ol><span style="font-size: 10pt"><tt>Codeblocks->Settings->Debugger->Default->Enable Watch Scripts = Unchecked</tt></span></ol>
 +
 
 +
===Other Info===
 
Links:
 
Links:
http://sourceware.org/gdb/onlinedocs/gdb/Python-API.html
 
http://sourceware.org/gdb/onlinedocs/gdb/Pretty-Printing.html
 
  
[u]To Do[/u]
+
[http://sourceware.org/gdb/onlinedocs/gdb/Python-API.html GDB Python API]
 +
 
 +
[http://sourceware.org/gdb/onlinedocs/gdb/Pretty-Printing.html 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?
 
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.
+
==See also==
 +
* [[Debugging with Code::Blocks]]
 +
* [[Debugger scripts]]
 +
* [[Configure GDB pretty printer for Msys2]]

Latest revision as of 11:36, 20 March 2021

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 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_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 of Codeblocks may prevent entering anything 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.

  1. Disable Codeblocks handling of watch values (needed only for versions older than 17.12):
    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?

See also