【问题标题】:Display smart pointers in eclipse cdt using gdb pretty printers使用 gdb 漂亮打印机在 eclipse cdt 中显示智能指针
【发布时间】:2016-05-31 16:40:01
【问题描述】:

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

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

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

【问题讨论】:

    标签: python c++ eclipse gdb smart-pointers


    【解决方案1】:

    按照您的链接,我完全按照迈克尔的描述进行了操作,并且效果很好。可能,您在应用更改时犯了一些错误。 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<:vector char std::allocator> >, std::allocator<:vector rtti std::allocator char> > >, (__gnu_cxx::_Lock_policy)2>' 我不确定这是否是个问题
    • 也许您使用标志 -fno-rtti 禁用了 gcc 的 RTTI?我没有收到任何调试器警告,使用 MinGW-w64 64bit gcc 版本 6.2.0。我的 g++ 选项是 -O0 -g3 -Wall -c -fmessage-length=0 -std=c++11 -D_FILE_OFFSET_BITS=64 -D__WXMSW__
    • 但是,如果您的调试器有问题,这应该不是问题,正如 here 所解释的那样。
    • 我又试了一次,现在一切正常,这很奇怪。我只是希望它不会再次发生,但我的编译器标志应该没问题: -std=c++14 -O0 -g -pedantic -Wall -Wundef -fmessage-length=0
    猜你喜欢
    • 2014-01-24
    • 2014-07-13
    • 2011-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-26
    • 1970-01-01
    相关资源
    最近更新 更多