python - 使用 gdb pretty-print 在 eclipse cdt 中显示智能指针

标签 python c++ eclipse gdb smart-pointers

当我调试我的 c++11 应用程序时,我想查看 unique_ptr 和 shared_ptr 指向的对象。但是使用 libstdc++ pretty-print ,只打印了一个带有地址和类似内容的字符串,但我无法展开它来查看它的内容。我已经尝试了以下解决方法,但对我不起作用:

https://sourceware.org/ml/gdb/2013-04/msg00042.html

谁能帮我解决这个问题。其实我认为这可能是一个非常基本的问题,所以我想知道是否没有办法这样做。但是在互联网上搜索我找不到任何提示......

最佳答案

按照您的链接,我完全按照 Michael 的描述进行操作,并且效果很好。可能您在应用更改时犯了一些错误。 libstdcxx/v6/printers.py 现在应该在第 103 - 174 行:

class SharedPointerPrinter:
    "Print a shared_ptr or weak_ptr"

    class _iterator:
        def __init__(self, sharedPointer):
            self.sharedPointer = sharedPointer
            self.managedValue = sharedPointer.val['_M_ptr']
            self.count = 0

        def __iter__(self):
            return self

        def next(self):
            if self.managedValue == 0:
                raise StopIteration
            self.count = self.count + 1
            if (self.count == 1):
                return ('Use count', self.sharedPointer.val['_M_refcount']['_M_pi']['_M_use_count'])
            elif (self.count == 2):
                return ('Weak count', self.sharedPointer.val['_M_refcount']['_M_pi']['_M_weak_count'] - 1)
            elif (self.count == 3):
                return ('Managed value', self.managedValue)
            else:
                raise StopIteration

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

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

    def to_string (self):
        state = 'empty'
        refcounts = self.val['_M_refcount']['_M_pi']
        if refcounts != 0:
            usecount = refcounts['_M_use_count']
            weakcount = refcounts['_M_weak_count']
            if usecount == 0:
                state = 'expired, weakcount %d' % weakcount
            else:
                state = 'usecount %d, weakcount %d' % (usecount, weakcount - 1)
        return '%s (%s) to %s' % (self.typename, state, self.val['_M_ptr'])

class UniquePointerPrinter:
    "Print a unique_ptr"

    class _iterator:
        def __init__(self, uniquePointer):
            self.uniquePointer = uniquePointer
            self.managedValue = uniquePointer.val['_M_t']['_M_head_impl']
            self.count = 0

        def __iter__(self):
            return self

        def next(self):
            if self.managedValue == 0 or self.count == 1:
                raise StopIteration
            self.count = self.count + 1
            return ('Managed value', self.managedValue)

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

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

    def to_string (self):
        v = self.val['_M_t']['_M_head_impl']
        return ('std::unique_ptr<%s> containing %s' % (str(v.type.target()),
                                                       str(v)))

亲切的问候

关于python - 使用 gdb pretty-print 在 eclipse cdt 中显示智能指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35499685/

相关文章:

python - 转换结构化数组中的日期元素

python - Azure 默认日志

python - 在 `variable` 中分配给 `with expression as variable` 的是什么?

c++ - 将用户输入的 int 作为大小,将 int 作为数组,然后显示数组,最后一个索引到第一个索引

android - 导入现有的 Android 项目,eclipse 弹出错误消息

python - Django 1.5->1.6 无法导入名称 zip_longest

c++ - 为什么在 "SND_SYNC"中使用 "QueueUserWorkItem"坏?

c++ - 模板参数与类型参数与非类型参数

Android无法启动 Activity ,错误膨胀类?

eclipse - 您认为什么是好的 Eclipse 支持?