在gdb中打印boost :: mpl :: string <...>类型

eth*_*ice 6 c++ boost gdb metaprogramming boost-mpl

我使用boost::mpl::string<...>类型广泛......以至于它会真的与调试有类型适合打印帮助gdb.

所以...而不是gdb像目前那样显示单个(多字符文字)组件......

boost::mpl::string<1668248165, 778856802, 778858343, ..., ..., 0, 0, 0, 0, 0, 0>
Run Code Online (Sandbox Code Playgroud)

它会显示等效的字符串值而不是......

boost::mpl::string<"The way out is through">
Run Code Online (Sandbox Code Playgroud)

我已经看过gdb用于漂亮打印STL容器的宏和python脚本gdb,但我找不到一个漂亮的打印boost::mpl字符串.有人能帮忙吗?


更新:我添加了一个+100赏金......我要找的是采用了最新的GDB支持通过Python漂亮的印刷(如描述的解决方案在这里对STL容器).

小智 7

这是我使用Boost-Pretty-Printer(https://github.com/ruediger/Boost-Pretty-Printer/wiki)的解决方案:

文件mpl_printers.py:

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
Run Code Online (Sandbox Code Playgroud)

文件register_printers.gdb:

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
Run Code Online (Sandbox Code Playgroud)
  • 在目录/ usr/local/share/gdb/python/boost中安装printers.py和上面的mpl_printers.py.
  • 确保在/ usr/local/share/gdb/python/boost中有__init__.py(空文件会这样做)
  • 在/ usr/local/share/gdb中安装上面的'register_printers.gdb'.
  • 在.gdbinit中添加'source /usr/local/share/gdb/register_printers.gdb'

(您可以选择不同的目录)

测试:

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

gdb测试-ex'b main'-ex'r'-ex'ps'-ex'c'-ex'q'

$ 1 =(boost :: mpl :: string)你好世界