LLDB数据格式化器和C阵列

时间:2017-10-02 14:32:16

标签: c++ lldb pretty-print

我想为自己的对象编写一个LLDB数据格式化程序,如下所示:

template <typename T, int n>
class StaticArray {
  T data_[n];
}

以下是我的合成数据格式化程序到目前为止的样子:

class StaticArrayProvider:
    def __init__(self, valobj, internal_dict):
        self.valobj = valobj
        self.data = self.valobj.GetChildMemberWithName('data_').GetChildAtIndex(0)
        self.data_type = self.data.GetType()
        self.type_size = self.data_type.GetByteSize()
        self.size = # ???

    def num_children(self):
        return self.size

    def get_child_index(self, name):
        try:
            return int(name.lstrip('[').rstrip(']'))
        except:
            return -1

    def get_child_at_index(self, index):
        if index < 0:
            return None
        if index >= self.num_children():
            return None
        try:
            offset = index * self.type_size
            return # ???
        except:
            return None

我不知道如何填补空白# ???。你有任何解决方案吗?

1 个答案:

答案 0 :(得分:1)

在lldb值系统中,GetNumChildren返回静态大小的数组的元素数,GetChildAtIndex获取该数组元素。由于每个模板实例化data_都是一个静态大小的数组,因此您的数据格式化程序可以在提供子项时转发data_。即你可以这样做:

self.data = self.valobj.GetChildMemberWithName('data_')

然后num_children只返回self.data.GetNumChildren()get_child_at_index返回self.data.GetChildAtIndex()

当lldb无法为您解决时,您只需计算偏移量和大小(例如,如果您有一个动态大小的数组或指向您将其视为数组的类型的指针。)