Pretty printed boost :: mpl :: string <...> types in gdb

I use the boost::mpl::string<...> types extensively, so that it really helps when debugging to have typed types printed in gdb .

So ... instead of gdb showing the individual (multi-character literals) components, as is currently happening ...

 boost::mpl::string<1668248165, 778856802, 778858343, ..., ..., 0, 0, 0, 0, 0, 0> 

Instead, the equivalent string value will be displayed ...

 boost::mpl::string<"The way out is through"> 

I saw gdb macros and python scripts for pretty-printed STL containers in gdb , but I could not find them for beautiful print boost::mpl . Can anyone help with this?


UPDATE: I added a +100 award ... I'm looking for a solution that uses the latest GDB support for pretty-printed work using python (as described here for STL containers).

+6
source share
1 answer

Here is my solution using Boost-Pretty-Printer ( https://github.com/ruediger/Boost-Pretty-Printer/wiki ):

Mpl_printers.py file:

 import printers import re import string import struct @printers.register_pretty_printer class BoostMplString: "Pretty Printer for boost::mpl::string" regex = re.compile('^boost::mpl::string<(.*)>$') @printers.static def supports(typename): return BoostMplString.regex.search(typename) def __init__(self, typename, value): self.typename = typename self.value = value def to_string(self): s = '' try: m = BoostMplString.regex.match(self.typename) args = string.split(m.group(1), ', ') for packed in args: i = int(packed) if i == 0: break r = '' while i != 0: i, c = divmod(i, 0x100) r += chr(c) s += r[::-1] except RuntimeError: s = '[Exception]' return '(boost::mpl::string) %s' % (s) def register_boost_mpl_printers(obj): "Register Boost Pretty Printers." pass 

Register_printers.gdb file:

 python # Add the following line in your .gdbinit: # source /usr/local/share/gdb/register_printers.gdb import sys sys.path.insert(0, '/usr/local/share/gdb/python') # You might have these, too # from libstdcxx.v6.printers import register_libstdcxx_printers from boost.printers import register_boost_printers from boost.mpl_printers import register_boost_mpl_printers # register_libstdcxx_printers(None) register_boost_printers(None) register_boost_mpl_printers(None) end 
  • Install printerers.py and higher mpl_printers.py in the / USR / local / share / gdb / python / momentum directory.
  • Make sure you have __init__.py in / usr / local / share / gdb / python / boost (an empty file will do this)
  • Set the above 'register_printers.gdb' to / usr / local / share / gdb.
  • Add 'source / usr / local / share / gdb / register_printers.gdb' to your .gdbinit

(You can choose different directories)

Test:

 #include <boost/mpl/string.hpp> int main() { boost::mpl::string<'hell','o wo','rld'> s; return 0; } 

gdb Test -ex 'b main' -ex 'r' -ex 'ps' -ex 'c' -ex 'q'

$ 1 = (boost :: mpl :: string) hello world

+7
source

Source: https://habr.com/ru/post/953113/


All Articles