xref: /openbsd-src/gnu/llvm/lldb/examples/summaries/cocoa/CFArray.py (revision f6aab3d83b51b91c24247ad2c2573574de475a82)
1061da546Spatrick"""
2061da546SpatrickLLDB AppKit formatters
3061da546Spatrick
4061da546SpatrickPart of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5061da546SpatrickSee https://llvm.org/LICENSE.txt for license information.
6061da546SpatrickSPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7061da546Spatrick"""
8061da546Spatrick# example summary provider for NSArray
9061da546Spatrick# the real summary is now C++ code built into LLDB
10061da546Spatrickimport lldb
11061da546Spatrickimport ctypes
12061da546Spatrickimport lldb.runtime.objc.objc_runtime
13061da546Spatrickimport lldb.formatters.metrics
14061da546Spatrickimport lldb.formatters.Logger
15061da546Spatrick
16061da546Spatrickstatistics = lldb.formatters.metrics.Metrics()
17061da546Spatrickstatistics.add_metric('invalid_isa')
18061da546Spatrickstatistics.add_metric('invalid_pointer')
19061da546Spatrickstatistics.add_metric('unknown_class')
20061da546Spatrickstatistics.add_metric('code_notrun')
21061da546Spatrick
22061da546Spatrick# much less functional than the other two cases below
23061da546Spatrick# just runs code to get to the count and then returns
24061da546Spatrick# no children
25061da546Spatrick
26061da546Spatrick
27061da546Spatrickclass NSArrayKVC_SynthProvider:
28061da546Spatrick
29061da546Spatrick    def adjust_for_architecture(self):
30061da546Spatrick        pass
31061da546Spatrick
32061da546Spatrick    def __init__(self, valobj, dict, params):
33061da546Spatrick        logger = lldb.formatters.Logger.Logger()
34061da546Spatrick        self.valobj = valobj
35061da546Spatrick        self.update()
36061da546Spatrick
37061da546Spatrick    def update(self):
38061da546Spatrick        logger = lldb.formatters.Logger.Logger()
39061da546Spatrick        self.adjust_for_architecture()
40061da546Spatrick
41061da546Spatrick    def num_children(self):
42061da546Spatrick        logger = lldb.formatters.Logger.Logger()
43061da546Spatrick        stream = lldb.SBStream()
44061da546Spatrick        self.valobj.GetExpressionPath(stream)
45061da546Spatrick        num_children_vo = self.valobj.CreateValueFromExpression(
46061da546Spatrick            "count", "(int)[" + stream.GetData() + " count]")
47061da546Spatrick        if num_children_vo.IsValid():
48061da546Spatrick            return num_children_vo.GetValueAsUnsigned(0)
49061da546Spatrick        return "<variable is not NSArray>"
50061da546Spatrick
51061da546Spatrick# much less functional than the other two cases below
52061da546Spatrick# just runs code to get to the count and then returns
53061da546Spatrick# no children
54061da546Spatrick
55061da546Spatrick
56061da546Spatrickclass NSArrayCF_SynthProvider:
57061da546Spatrick
58061da546Spatrick    def adjust_for_architecture(self):
59061da546Spatrick        pass
60061da546Spatrick
61061da546Spatrick    def __init__(self, valobj, dict, params):
62061da546Spatrick        logger = lldb.formatters.Logger.Logger()
63061da546Spatrick        self.valobj = valobj
64061da546Spatrick        self.sys_params = params
65061da546Spatrick        if not (self.sys_params.types_cache.ulong):
66061da546Spatrick            self.sys_params.types_cache.ulong = self.valobj.GetType(
67061da546Spatrick            ).GetBasicType(lldb.eBasicTypeUnsignedLong)
68061da546Spatrick        self.update()
69061da546Spatrick
70061da546Spatrick    def update(self):
71061da546Spatrick        logger = lldb.formatters.Logger.Logger()
72061da546Spatrick        self.adjust_for_architecture()
73061da546Spatrick
74061da546Spatrick    def num_children(self):
75061da546Spatrick        logger = lldb.formatters.Logger.Logger()
76061da546Spatrick        num_children_vo = self.valobj.CreateChildAtOffset(
77061da546Spatrick            "count", self.sys_params.cfruntime_size, self.sys_params.types_cache.ulong)
78061da546Spatrick        return num_children_vo.GetValueAsUnsigned(0)
79061da546Spatrick
80061da546Spatrick
81061da546Spatrickclass NSArrayI_SynthProvider:
82061da546Spatrick
83061da546Spatrick    def adjust_for_architecture(self):
84061da546Spatrick        pass
85061da546Spatrick
86061da546Spatrick    def __init__(self, valobj, dict, params):
87061da546Spatrick        logger = lldb.formatters.Logger.Logger()
88061da546Spatrick        self.valobj = valobj
89061da546Spatrick        self.sys_params = params
90061da546Spatrick        if not(self.sys_params.types_cache.long):
91061da546Spatrick            self.sys_params.types_cache.long = self.valobj.GetType(
92061da546Spatrick            ).GetBasicType(lldb.eBasicTypeLong)
93061da546Spatrick        self.update()
94061da546Spatrick
95061da546Spatrick    def update(self):
96061da546Spatrick        logger = lldb.formatters.Logger.Logger()
97061da546Spatrick        self.adjust_for_architecture()
98061da546Spatrick
99061da546Spatrick    # skip the isa pointer and get at the size
100061da546Spatrick    def num_children(self):
101061da546Spatrick        logger = lldb.formatters.Logger.Logger()
102061da546Spatrick        count = self.valobj.CreateChildAtOffset(
103061da546Spatrick            "count",
104061da546Spatrick            self.sys_params.pointer_size,
105061da546Spatrick            self.sys_params.types_cache.long)
106061da546Spatrick        return count.GetValueAsUnsigned(0)
107061da546Spatrick
108061da546Spatrick
109061da546Spatrickclass NSArrayM_SynthProvider:
110061da546Spatrick
111061da546Spatrick    def adjust_for_architecture(self):
112061da546Spatrick        pass
113061da546Spatrick
114061da546Spatrick    def __init__(self, valobj, dict, params):
115061da546Spatrick        logger = lldb.formatters.Logger.Logger()
116061da546Spatrick        self.valobj = valobj
117061da546Spatrick        self.sys_params = params
118061da546Spatrick        if not(self.sys_params.types_cache.long):
119061da546Spatrick            self.sys_params.types_cache.long = self.valobj.GetType(
120061da546Spatrick            ).GetBasicType(lldb.eBasicTypeLong)
121061da546Spatrick        self.update()
122061da546Spatrick
123061da546Spatrick    def update(self):
124061da546Spatrick        logger = lldb.formatters.Logger.Logger()
125061da546Spatrick        self.adjust_for_architecture()
126061da546Spatrick
127061da546Spatrick    # skip the isa pointer and get at the size
128061da546Spatrick    def num_children(self):
129061da546Spatrick        logger = lldb.formatters.Logger.Logger()
130061da546Spatrick        count = self.valobj.CreateChildAtOffset(
131061da546Spatrick            "count",
132061da546Spatrick            self.sys_params.pointer_size,
133061da546Spatrick            self.sys_params.types_cache.long)
134061da546Spatrick        return count.GetValueAsUnsigned(0)
135061da546Spatrick
136061da546Spatrick# this is the actual synth provider, but is just a wrapper that checks
137061da546Spatrick# whether valobj is an instance of __NSArrayI or __NSArrayM and sets up an
138061da546Spatrick# appropriate backend layer to do the computations
139061da546Spatrick
140061da546Spatrick
141061da546Spatrickclass NSArray_SynthProvider:
142061da546Spatrick
143061da546Spatrick    def adjust_for_architecture(self):
144061da546Spatrick        pass
145061da546Spatrick
146061da546Spatrick    def __init__(self, valobj, dict):
147061da546Spatrick        logger = lldb.formatters.Logger.Logger()
148061da546Spatrick        self.valobj = valobj
149061da546Spatrick        self.adjust_for_architecture()
150061da546Spatrick        self.error = False
151061da546Spatrick        self.wrapper = self.make_wrapper()
152061da546Spatrick        self.invalid = (self.wrapper is None)
153061da546Spatrick
154061da546Spatrick    def num_children(self):
155061da546Spatrick        logger = lldb.formatters.Logger.Logger()
156061da546Spatrick        if self.wrapper is None:
157061da546Spatrick            return 0
158061da546Spatrick        return self.wrapper.num_children()
159061da546Spatrick
160061da546Spatrick    def update(self):
161061da546Spatrick        logger = lldb.formatters.Logger.Logger()
162061da546Spatrick        if self.wrapper is None:
163061da546Spatrick            return
164061da546Spatrick        self.wrapper.update()
165061da546Spatrick
166061da546Spatrick    # this code acts as our defense against NULL and uninitialized
167061da546Spatrick    # NSArray pointers, which makes it much longer than it would be otherwise
168061da546Spatrick    def make_wrapper(self):
169061da546Spatrick        logger = lldb.formatters.Logger.Logger()
170061da546Spatrick        if self.valobj.GetValueAsUnsigned() == 0:
171061da546Spatrick            self.error = True
172061da546Spatrick            return lldb.runtime.objc.objc_runtime.InvalidPointer_Description(
173061da546Spatrick                True)
174061da546Spatrick        else:
175061da546Spatrick            global statistics
176061da546Spatrick            class_data, wrapper = lldb.runtime.objc.objc_runtime.Utilities.prepare_class_detection(
177061da546Spatrick                self.valobj, statistics)
178061da546Spatrick            if wrapper:
179061da546Spatrick                self.error = True
180061da546Spatrick                return wrapper
181061da546Spatrick
182061da546Spatrick        name_string = class_data.class_name()
183061da546Spatrick
184061da546Spatrick        logger >> "Class name is " + str(name_string)
185061da546Spatrick
186061da546Spatrick        if name_string == '__NSArrayI':
187061da546Spatrick            wrapper = NSArrayI_SynthProvider(
188061da546Spatrick                self.valobj, dict, class_data.sys_params)
189061da546Spatrick            statistics.metric_hit('code_notrun', self.valobj.GetName())
190061da546Spatrick        elif name_string == '__NSArrayM':
191061da546Spatrick            wrapper = NSArrayM_SynthProvider(
192061da546Spatrick                self.valobj, dict, class_data.sys_params)
193061da546Spatrick            statistics.metric_hit('code_notrun', self.valobj.GetName())
194061da546Spatrick        elif name_string == '__NSCFArray':
195061da546Spatrick            wrapper = NSArrayCF_SynthProvider(
196061da546Spatrick                self.valobj, dict, class_data.sys_params)
197061da546Spatrick            statistics.metric_hit('code_notrun', self.valobj.GetName())
198061da546Spatrick        else:
199061da546Spatrick            wrapper = NSArrayKVC_SynthProvider(
200061da546Spatrick                self.valobj, dict, class_data.sys_params)
201061da546Spatrick            statistics.metric_hit(
202061da546Spatrick                'unknown_class', str(
203061da546Spatrick                    self.valobj.GetName()) + " seen as " + name_string)
204061da546Spatrick        return wrapper
205061da546Spatrick
206061da546Spatrick
207061da546Spatrickdef CFArray_SummaryProvider(valobj, dict):
208061da546Spatrick    logger = lldb.formatters.Logger.Logger()
209061da546Spatrick    provider = NSArray_SynthProvider(valobj, dict)
210061da546Spatrick    if not provider.invalid:
211061da546Spatrick        if provider.error:
212061da546Spatrick            return provider.wrapper.message()
213061da546Spatrick        try:
214061da546Spatrick            summary = int(provider.num_children())
215061da546Spatrick        except:
216061da546Spatrick            summary = None
217061da546Spatrick        logger >> "provider gave me " + str(summary)
218061da546Spatrick        if summary is None:
219061da546Spatrick            summary = '<variable is not NSArray>'
220*f6aab3d8Srobert        elif isinstance(summary, str):
221061da546Spatrick            pass
222061da546Spatrick        else:
223061da546Spatrick            # we format it like it were a CFString to make it look the same as
224061da546Spatrick            # the summary from Xcode
225061da546Spatrick            summary = '@"' + str(summary) + \
226061da546Spatrick                (" objects" if summary != 1 else " object") + '"'
227061da546Spatrick        return summary
228061da546Spatrick    return 'Summary Unavailable'
229061da546Spatrick
230061da546Spatrick
231061da546Spatrickdef __lldb_init_module(debugger, dict):
232061da546Spatrick    debugger.HandleCommand(
233061da546Spatrick        "type summary add -F CFArray.CFArray_SummaryProvider NSArray CFArrayRef CFMutableArrayRef")
234