与lldb漂亮的打印机共享ptr地图

时间:2017-11-16 22:45:51

标签: c++ shared-ptr lldb stdmap

我有一个std :: map,我使用shared_ptrs作为键:

std::map<shared_ptr<object>, shared_ptr<object>>

然而,在调试时,我无法使用macOS上的lldb或linux上的gdb打开这些映射。使用gdb,通过修改文件libstdcxx / v6 / printers.py并将迭代器添加到SharedPointerPrinter类(如下所示的代码),在Ubuntu上修复了此问题。但是,我不知道如何解决lldb的这个问题。如果有人可以帮助我,或指出我的方向是正确的。

如果您需要其他信息以帮助我解决此问题,请告诉我。我很乐意提供。

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'])

1 个答案:

答案 0 :(得分:0)

你可以使用lldb的Python数据格式化程序做同样的事情,如本页所述:

http://lldb.llvm.org/varformats.html

lldb具有std :: map和std :: shared_ptr的数据格式化程序,用于clang的STL版本。例如,我看到了:

(lldb) fr v a_map
(std::__1::map<std::__1::shared_ptr<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, std::__1::shared_ptr<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, std::__1::less<std::__1::shared_ptr<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >, std::__1::allocator<std::__1::pair<const std::__1::shared_ptr<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, std::__1::shared_ptr<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > > >) a_map = size=1 {
  [0] = {
    first = "some text" strong=2 weak=1 {
      __ptr_ = "some text"
    }
    second = "other text" strong=2 weak=1 {
      __ptr_ = "other text"
    }
  }
}

伙计,那些C ++类型很冗长!

gcc的STL也有一些格式化程序,但我不太了解那些格式化程序的状态。

IIUC数据格式化程序可以在gdb中的表达式中使用。在lldb中,frame variable命令将接受简单的结构访问表达式,因此您可以这样做:

(lldb) frame var a_map[0].first
(const std::__1::shared_ptr<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >) a_map[0].first = "some text" strong=2 weak=1 {
  __ptr_ = "some text"
}

但是你不能在完整表达式解析器中使用数据格式化器生成的子代。