generic-array icon indicating copy to clipboard operation
generic-array copied to clipboard

LLDB Debugging

Open makavity opened this issue 2 months ago • 0 comments

Hello, just done LLDB SyntheticProvider for GenericArray, it is not ideal, but works :)

import lldb

class GenericArraySyntheticProvider:
    def __init__(self, valobj, internal_dict):
        self.valobj = valobj
        self.count = 0
        self.extract_values()

    def extract_values(self):
        self.values = []
        self.types = []
        self.count = 0
        # Assuming data is the root field containing the array structure
        data_valobj = self.valobj.GetChildMemberWithName('data')
        self.traverse(data_valobj)

    def traverse(self, valobj):
        if not valobj.IsValid():
            return
        if valobj.GetNumChildren() == 0:
            return
        for i in range(valobj.GetNumChildren()):
            child = valobj.GetChildAtIndex(i)
            if child.IsValid():
                data_child = child.GetChildMemberWithName('data')
                if data_child.IsValid():
                    self.values.append(data_child.GetData())
                    self.types.append(data_child.GetType())
                    self.count += 1
                self.traverse(child)

    def num_children(self):
        return self.count

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

    def get_child_at_index(self, index):
        if index < 0 or index >= self.count:
            return None

        value = self.values[index]
        typo = self.types[index]
        return self.valobj.CreateValueFromData(f"[{index}]", value, typo)

    def update(self):
        # Re-extract values in case the underlying object has changed
        self.extract_values()

    def has_children(self):
        return True

def __lldb_init_module(debugger, internal_dict):
    debugger.HandleCommand(f'type synthetic add -l { __name__ }.GenericArraySyntheticProvider -x "^generic_array::GenericArray<.*>$"')

And command to add it: command script import ~/.lldb/scripts/GenericArray.py in .lldbinit

image

makavity avatar Apr 17 '24 07:04 makavity