c++ - 在 gdb 上漂亮地打印 boost::unordered_map

标签 c++ boost gdb

最近我开始在我的系统上使用优秀的 boost::unordered_map,但有一个缺点:我不知道如何检查它的内容。在 gdb 上打印它给了我一个 table_ 和一个 buckets_,但还没有找到项目在哪里。有人知道这件事吗?

最佳答案

对于那些想要打印机的人,我已经设法制造了一台。这是代码:

class BoostUnorderedMapPrinter:
    "prints a 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"

关于c++ - 在 gdb 上漂亮地打印 boost::unordered_map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2804641/

相关文章:

c - 访问结构体中的函数时出现段错误

c++ - 区分零和负零

c++ - Opengl C++ : How do I make parts of a texture transparent?

c++ - 将特征矩阵映射到 C 数组

c++ - ‘binary_semaphore’ 尚未在 ‘std’ 中声明

c++ - 在 std::map<..., boost::any> 中初始化子图

c++读取时同步共享内存

c++ - gdb - 防止在捕获/重新抛出情况下丢失回溯

c++ - `boost::any` 和 `std::any` 之间的差异

debugging - 如何在 gdb 上进行 grep 打印