6

When I debug my c++11 application, I want to see the objects unique_ptr and shared_ptr are pointing to. But using libstdc++ pretty printers, only a string with address and similar stuff is printed, but I can't expand it to view its content. I already tried the following workaround but I did not work for me:

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

Can anybody help me with that. Actually I think this might be a pretty basic problem, so I'm wondering wether there is no way to do so. But searching the internet I couldn't find any hint...

Johannes91
  • 120
  • 10

1 Answers1

4

Following your link, I did exactly what Michael described, and it works fine. Probably, you did some mistake in applying the changes. The libstdcxx/v6/printers.py should now have in lines 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)))

Kind regards

xamid
  • 440
  • 11
  • 25
  • Thanks, it works for me now. But I get some debugger warnings: warning: RTTI symbol not found for class 'std::_Sp_counted_ptr_inplace >, std::allocator > >, (__gnu_cxx::_Lock_policy)2>' I'm not sure if that might be a problem – Johannes91 Oct 19 '16 at 05:44
  • Maybe you disabled RTTI for gcc using flag -fno-rtti? I am not getting any debugger warnings, using MinGW-w64 64bit gcc version 6.2.0. My g++ options are -O0 -g3 -Wall -c -fmessage-length=0 -std=c++11 -D_FILE_OFFSET_BITS=64 -D__WXMSW__ – xamid Oct 20 '16 at 05:36
  • However, if your debugger is buggy, this shouldn't really be a problem as explained [here](http://stackoverflow.com/questions/12986261/warning-message-rtti-symbol-not-found-when-using-boostiostreams/12991374#12991374). – xamid Oct 20 '16 at 05:50
  • I tried it again and now everything is ok, that's pretty weird. I just hope it doesn't happen again, but my compiler flags should be ok: -std=c++14 -O0 -g -pedantic -Wall -Wundef -fmessage-length=0 – Johannes91 Oct 20 '16 at 06:15