在eclipse cdt中使用gdb漂亮打印器显示智能指针

6
当我调试我的C++11应用程序时,我想查看unique_ptr和shared_ptr指向的对象。但是使用libstdc++漂亮的打印机,只会打印一个带有地址和类似内容的字符串,但我无法展开它以查看其内容。我已经尝试了以下解决方法,但对我没有用:

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

有人能帮我吗?实际上,我认为这可能是一个相当基本的问题,所以我想知道是否没有办法这样做。但是在搜索互联网时,我找不到任何提示...

1个回答

4

根据你提供的链接,我按照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)))

亲切的问候

谢谢,现在它对我有效了。但是我收到了一些调试器警告:警告:未找到类'std::_Sp_counted_ptr_inplace<std::vector<unsigned char, std::allocator<unsigned char> >, std::allocator<std::vector<unsigned char, std::allocator<unsigned char> > >, (__gnu_cxx::_Lock_policy)2>'的RTTI符号。我不确定那是否可能会成为一个问题。 - Johannes91
也许您使用了-fno-rtti标志禁用了gcc的RTTI功能?我没有收到任何调试器警告,使用MinGW-w64 64位gcc版本6.2.0。我的g++选项是-O0 -g3 -Wall -c -fmessage-length=0 -std=c++11 -D_FILE_OFFSET_BITS=64 -D__WXMSW__。 - xamid
然而,如果你的调试器有缺陷,这其实不应该成为一个问题,如此解释 - xamid
我又试了一遍,现在一切都正常了,这很奇怪。我只希望不要再发生了,但我的编译器标志应该没问题:-std=c++14 -O0 -g -pedantic -Wall -Wundef -fmessage-length=0 - Johannes91

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接