【问题标题】:pretty printing boost::mpl::string<...> types in gdb漂亮的打印 boost::mpl::string<...> 类型在 gdb
【发布时间】:2013-09-07 10:03:14
【问题描述】:

我广泛使用boost::mpl::string&lt;...&gt; 类型...足以真正帮助调试在gdb 中漂亮地打印类型。

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

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

它将显示等效的字符串值...

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

我在gdb 中看到了用于漂亮打印STL 容器的gdb 宏和python 脚本,但我找不到用于漂亮打印boost::mpl 字符串的脚本。有人可以帮忙吗?


更新:我添加了 +100 赏金...我正在寻找一种解决方案,该解决方案利用最新的 GDB 支持通过 python 进行漂亮打印(如here 所述,用于 STL 容器)。

【问题讨论】:

    标签: c++ boost gdb metaprogramming boost-mpl


    【解决方案1】:

    这是我使用 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
    

    文件 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
    
    • 在目录下安装printers.py和上面的mpl_printers.py /usr/local/share/gdb/python/boost.
    • 确保在 /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;
    }
    

    gdb 测试 -ex 'b main' -ex 'r' -ex 'p s' -ex 'c' -ex 'q'

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

    【讨论】:

    • 像魅力一样工作! 现在我可以使用 python 使用相同的方法轻松美化其他类型名称。谢谢。
    • @etherice 请在某一天发布您的打印机。
    • 为了澄清说明中的几个部分:printers.pympl_printers.py 应该放在boost 子目录中(即/usr/local/share/gdb/python/boost),因此register_printers.gdb 中的模块名称是有效的。此外,boost 子目录必须包含一个__init__.py 文件(可以为空),以便python 将其识别为包含模块的目录(否则,您将收到ImportError: No module named boost.printers 错误)。
    • 迪特-NP。再次感谢您提供了一个优雅的解决方案,该解决方案完全满足了我们的要求。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多