【问题标题】:How to add new pretty printers to an existing list of printer definitions?如何将新的漂亮打印机添加到现有的打印机定义列表中?
【发布时间】:2014-01-21 09:49:05
【问题描述】:

我想通过更新我的 std::pretty 打印机或 boost::prettyprinters 的现有 printers.py 文件来添加新的漂亮打印机。 使用以下链接适当地设置它们: https://sourceware.org/gdb/wiki/STLSupport https://github.com/ruediger/Boost-Pretty-Printer

我也阅读了他们的教程来添加新的打印机,但不知何故未能很好地理解。也有机会研究类似的线程: pretty printing boost::mpl::string<...> types in gdbPretty printing boost::unordered_map on gdb

很高兴知道如何添加在我的 printers.py 文件中注册的上述 boost::unorder_map。我对 boost 目录中的 printers.py 文件进行了以下修改

@_register_printer
class BoostUnorderedMapPrinter:
"Pretty printer for a boost::unordered_map"
printer_name = 'boost::unordered_map'
version = '1.40'
type_name_re = '^boost::unordered_map$'

class _iterator:
    def __init__ (self, fields):
        type_1 = fields.val.type.template_argument(0)
        type_2 = fields.val.type.template_argument(1)
        self.buckets = fields.val['table_']['buckets_']
        self.bucket_count = fields.val['table_']['bucket_count_']
        self.current_bucket = 0
        pair = "std::pair<%s const, %s>" % (type_1, type_2)
        self.pair_pointer = gdb.lookup_type(pair).pointer()
        self.base_pointer = gdb.lookup_type("boost::unordered_detail::value_base< %s >" % pair).pointer()
        self.node_pointer = gdb.lookup_type("boost::unordered_detail::hash_node<std::allocator< %s >, boost::unordered_detail::ungrouped>" % pair).pointer()
        self.node = self.buckets[self.current_bucket]['next_']

    def __iter__(self):
        return self

    def next(self):
        while not self.node:
            self.current_bucket = self.current_bucket + 1
            if self.current_bucket >= self.bucket_count:
                raise StopIteration
            self.node = self.buckets[self.current_bucket]['next_']

        iterator = self.node.cast(self.node_pointer).cast(self.base_pointer).cast(self.pair_pointer).dereference()   
        self.node = self.node['next_']

        return ('%s' % iterator['first'], iterator['second'])

def __init__(self, val):
    self.val = val

def children(self):
    return self._iterator(self)

def to_string(self):
    return "boost::unordered_map"

不知何故,它似​​乎无法识别此类。

提前致谢

【问题讨论】:

  • 您需要围绕 boost 库创建一个 python 包装器,可能使用 cython。
  • 不,这与在 gdb 中运行的 python 代码有关。为此,您不需要用于 boost 或 cython 的 python 包装器。

标签: python gdb pretty-print


【解决方案1】:

github 上的 boost 打印代码使用了一个装饰器来说明给定的打印机应该被注册,然后在类中的一些字段来控制注册:

@_register_printer
class BoostIteratorRange:
...
printer_name = 'boost::iterator_range'
version = '1.40'
type_name_re = '^boost::iterator_range<.*>$'

所以我想您可以将这些添加到其他 SO 帖子中给出的示例代码中以进行设置。

您也可以自己手写注册码。

【讨论】:

  • 我对 boost 目录中的 printers.py 文件进行了更改,如上所示(编辑部分),但我的无序地图仍然无法打印可理解的结果。我做错什么了吗?
【解决方案2】:

可能不是最好的方法,但它适用于我的快速调试会话:

import gdb
import re

def lookup_type(val)
   resolved_type = str(val.type.unqualified().strip_typedefs())
   if (re.search("^boost::unordered_map>.*>$", resolved_type):
      return BoostUnorderedMapPrinter()

gdb.pretty_printers.append (lookup_type)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-20
    • 1970-01-01
    • 1970-01-01
    • 2013-08-08
    • 1970-01-01
    相关资源
    最近更新 更多