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 NSDictionary
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# despite the similary to synthetic children providers, these classes are not
23061da546Spatrick# trying to provide anything but the count for an NSDictionary, so they need not
24061da546Spatrick# obey the interface specification for synthetic children providers
25061da546Spatrick
26061da546Spatrick
27061da546Spatrickclass NSCFDictionary_SummaryProvider:
28061da546Spatrick
29061da546Spatrick    def adjust_for_architecture(self):
30061da546Spatrick        pass
31061da546Spatrick
32061da546Spatrick    def __init__(self, valobj, params):
33061da546Spatrick        logger = lldb.formatters.Logger.Logger()
34061da546Spatrick        self.valobj = valobj
35061da546Spatrick        self.sys_params = params
36061da546Spatrick        if not(self.sys_params.types_cache.NSUInteger):
37061da546Spatrick            if self.sys_params.is_64_bit:
38061da546Spatrick                self.sys_params.types_cache.NSUInteger = self.valobj.GetType(
39061da546Spatrick                ).GetBasicType(lldb.eBasicTypeUnsignedLong)
40061da546Spatrick            else:
41061da546Spatrick                self.sys_params.types_cache.NSUInteger = self.valobj.GetType(
42061da546Spatrick                ).GetBasicType(lldb.eBasicTypeUnsignedInt)
43061da546Spatrick        self.update()
44061da546Spatrick
45061da546Spatrick    def update(self):
46061da546Spatrick        logger = lldb.formatters.Logger.Logger()
47061da546Spatrick        self.adjust_for_architecture()
48061da546Spatrick
49061da546Spatrick    # empirically determined on both 32 and 64bit desktop Mac OS X
50061da546Spatrick    # probably boils down to 2 pointers and 4 bytes of data, but
51061da546Spatrick    # the description of __CFDictionary is not readily available so most
52061da546Spatrick    # of this is guesswork, plain and simple
53061da546Spatrick    def offset(self):
54061da546Spatrick        logger = lldb.formatters.Logger.Logger()
55061da546Spatrick        if self.sys_params.is_64_bit:
56061da546Spatrick            return 20
57061da546Spatrick        else:
58061da546Spatrick            return 12
59061da546Spatrick
60061da546Spatrick    def num_children(self):
61061da546Spatrick        logger = lldb.formatters.Logger.Logger()
62061da546Spatrick        num_children_vo = self.valobj.CreateChildAtOffset(
63061da546Spatrick            "count", self.offset(), self.sys_params.types_cache.NSUInteger)
64061da546Spatrick        return num_children_vo.GetValueAsUnsigned(0)
65061da546Spatrick
66061da546Spatrick
67061da546Spatrickclass NSDictionaryI_SummaryProvider:
68061da546Spatrick
69061da546Spatrick    def adjust_for_architecture(self):
70061da546Spatrick        pass
71061da546Spatrick
72061da546Spatrick    def __init__(self, valobj, params):
73061da546Spatrick        logger = lldb.formatters.Logger.Logger()
74061da546Spatrick        self.valobj = valobj
75061da546Spatrick        self.sys_params = params
76061da546Spatrick        if not(self.sys_params.types_cache.NSUInteger):
77061da546Spatrick            if self.sys_params.is_64_bit:
78061da546Spatrick                self.sys_params.types_cache.NSUInteger = self.valobj.GetType(
79061da546Spatrick                ).GetBasicType(lldb.eBasicTypeUnsignedLong)
80061da546Spatrick            else:
81061da546Spatrick                self.sys_params.types_cache.NSUInteger = self.valobj.GetType(
82061da546Spatrick                ).GetBasicType(lldb.eBasicTypeUnsignedInt)
83061da546Spatrick        self.update()
84061da546Spatrick
85061da546Spatrick    def update(self):
86061da546Spatrick        logger = lldb.formatters.Logger.Logger()
87061da546Spatrick        self.adjust_for_architecture()
88061da546Spatrick
89061da546Spatrick    # we just need to skip the ISA and the count immediately follows
90061da546Spatrick    def offset(self):
91061da546Spatrick        logger = lldb.formatters.Logger.Logger()
92061da546Spatrick        return self.sys_params.pointer_size
93061da546Spatrick
94061da546Spatrick    def num_children(self):
95061da546Spatrick        logger = lldb.formatters.Logger.Logger()
96061da546Spatrick        num_children_vo = self.valobj.CreateChildAtOffset(
97061da546Spatrick            "count", self.offset(), self.sys_params.types_cache.NSUInteger)
98061da546Spatrick        value = num_children_vo.GetValueAsUnsigned(0)
99061da546Spatrick        if value is not None:
100061da546Spatrick            # the MS6bits on immutable dictionaries seem to be taken by the LSB of capacity
101061da546Spatrick            # not sure if it is a bug or some weird sort of feature, but masking that out
102061da546Spatrick            # gets the count right
103061da546Spatrick            if self.sys_params.is_64_bit:
104061da546Spatrick                value = value & ~0xFC00000000000000
105061da546Spatrick            else:
106061da546Spatrick                value = value & ~0xFC000000
107061da546Spatrick        return value
108061da546Spatrick
109061da546Spatrick
110061da546Spatrickclass NSDictionaryM_SummaryProvider:
111061da546Spatrick
112061da546Spatrick    def adjust_for_architecture(self):
113061da546Spatrick        pass
114061da546Spatrick
115061da546Spatrick    def __init__(self, valobj, params):
116061da546Spatrick        logger = lldb.formatters.Logger.Logger()
117061da546Spatrick        self.valobj = valobj
118061da546Spatrick        self.sys_params = params
119061da546Spatrick        if not(self.sys_params.types_cache.NSUInteger):
120061da546Spatrick            if self.sys_params.is_64_bit:
121061da546Spatrick                self.sys_params.types_cache.NSUInteger = self.valobj.GetType(
122061da546Spatrick                ).GetBasicType(lldb.eBasicTypeUnsignedLong)
123061da546Spatrick            else:
124061da546Spatrick                self.sys_params.types_cache.NSUInteger = self.valobj.GetType(
125061da546Spatrick                ).GetBasicType(lldb.eBasicTypeUnsignedInt)
126061da546Spatrick        self.update()
127061da546Spatrick
128061da546Spatrick    def update(self):
129061da546Spatrick        logger = lldb.formatters.Logger.Logger()
130061da546Spatrick        self.adjust_for_architecture()
131061da546Spatrick
132061da546Spatrick    # we just need to skip the ISA and the count immediately follows
133061da546Spatrick    def offset(self):
134061da546Spatrick        return self.sys_params.pointer_size
135061da546Spatrick
136061da546Spatrick    def num_children(self):
137061da546Spatrick        logger = lldb.formatters.Logger.Logger()
138061da546Spatrick        num_children_vo = self.valobj.CreateChildAtOffset(
139061da546Spatrick            "count", self.offset(), self.sys_params.types_cache.NSUInteger)
140061da546Spatrick        value = num_children_vo.GetValueAsUnsigned(0)
141061da546Spatrick        if value is not None:
142061da546Spatrick            # the MS6bits on immutable dictionaries seem to be taken by the LSB of capacity
143061da546Spatrick            # not sure if it is a bug or some weird sort of feature, but masking that out
144061da546Spatrick            # gets the count right
145061da546Spatrick            if self.sys_params.is_64_bit:
146061da546Spatrick                value = value & ~0xFC00000000000000
147061da546Spatrick            else:
148061da546Spatrick                value = value & ~0xFC000000
149061da546Spatrick        return value
150061da546Spatrick
151061da546Spatrick
152061da546Spatrickclass NSDictionaryUnknown_SummaryProvider:
153061da546Spatrick
154061da546Spatrick    def adjust_for_architecture(self):
155061da546Spatrick        pass
156061da546Spatrick
157061da546Spatrick    def __init__(self, valobj, params):
158061da546Spatrick        logger = lldb.formatters.Logger.Logger()
159061da546Spatrick        self.valobj = valobj
160061da546Spatrick        self.sys_params = params
161061da546Spatrick        self.update()
162061da546Spatrick
163061da546Spatrick    def update(self):
164061da546Spatrick        logger = lldb.formatters.Logger.Logger()
165061da546Spatrick        self.adjust_for_architecture()
166061da546Spatrick
167061da546Spatrick    def num_children(self):
168061da546Spatrick        logger = lldb.formatters.Logger.Logger()
169061da546Spatrick        stream = lldb.SBStream()
170061da546Spatrick        self.valobj.GetExpressionPath(stream)
171061da546Spatrick        num_children_vo = self.valobj.CreateValueFromExpression(
172061da546Spatrick            "count", "(int)[" + stream.GetData() + " count]")
173061da546Spatrick        if num_children_vo.IsValid():
174061da546Spatrick            return num_children_vo.GetValueAsUnsigned(0)
175061da546Spatrick        return '<variable is not NSDictionary>'
176061da546Spatrick
177061da546Spatrick
178061da546Spatrickdef GetSummary_Impl(valobj):
179061da546Spatrick    logger = lldb.formatters.Logger.Logger()
180061da546Spatrick    global statistics
181061da546Spatrick    class_data, wrapper = lldb.runtime.objc.objc_runtime.Utilities.prepare_class_detection(
182061da546Spatrick        valobj, statistics)
183061da546Spatrick    if wrapper:
184061da546Spatrick        return wrapper
185061da546Spatrick
186061da546Spatrick    name_string = class_data.class_name()
187061da546Spatrick
188061da546Spatrick    logger >> "class name is: " + str(name_string)
189061da546Spatrick
190061da546Spatrick    if name_string == '__NSCFDictionary':
191061da546Spatrick        wrapper = NSCFDictionary_SummaryProvider(valobj, class_data.sys_params)
192061da546Spatrick        statistics.metric_hit('code_notrun', valobj)
193061da546Spatrick    elif name_string == '__NSDictionaryI':
194061da546Spatrick        wrapper = NSDictionaryI_SummaryProvider(valobj, class_data.sys_params)
195061da546Spatrick        statistics.metric_hit('code_notrun', valobj)
196061da546Spatrick    elif name_string == '__NSDictionaryM':
197061da546Spatrick        wrapper = NSDictionaryM_SummaryProvider(valobj, class_data.sys_params)
198061da546Spatrick        statistics.metric_hit('code_notrun', valobj)
199061da546Spatrick    else:
200061da546Spatrick        wrapper = NSDictionaryUnknown_SummaryProvider(
201061da546Spatrick            valobj, class_data.sys_params)
202061da546Spatrick        statistics.metric_hit(
203061da546Spatrick            'unknown_class',
204061da546Spatrick            valobj.GetName() +
205061da546Spatrick            " seen as " +
206061da546Spatrick            name_string)
207061da546Spatrick    return wrapper
208061da546Spatrick
209061da546Spatrick
210061da546Spatrickdef CFDictionary_SummaryProvider(valobj, dict):
211061da546Spatrick    logger = lldb.formatters.Logger.Logger()
212061da546Spatrick    provider = GetSummary_Impl(valobj)
213061da546Spatrick    if provider is not None:
214061da546Spatrick        if isinstance(
215061da546Spatrick                provider,
216061da546Spatrick                lldb.runtime.objc.objc_runtime.SpecialSituation_Description):
217061da546Spatrick            return provider.message()
218061da546Spatrick        try:
219061da546Spatrick            summary = provider.num_children()
220061da546Spatrick        except:
221061da546Spatrick            summary = None
222061da546Spatrick        logger >> "got summary " + str(summary)
223061da546Spatrick        if summary is None:
224061da546Spatrick            return '<variable is not NSDictionary>'
225*f6aab3d8Srobert        if isinstance(summary, str):
226061da546Spatrick            return summary
227061da546Spatrick        return str(summary) + (" key/value pairs" if summary !=
228061da546Spatrick                               1 else " key/value pair")
229061da546Spatrick    return 'Summary Unavailable'
230061da546Spatrick
231061da546Spatrick
232061da546Spatrickdef CFDictionary_SummaryProvider2(valobj, dict):
233061da546Spatrick    logger = lldb.formatters.Logger.Logger()
234061da546Spatrick    provider = GetSummary_Impl(valobj)
235061da546Spatrick    if provider is not None:
236061da546Spatrick        if isinstance(
237061da546Spatrick                provider,
238061da546Spatrick                lldb.runtime.objc.objc_runtime.SpecialSituation_Description):
239061da546Spatrick            return provider.message()
240061da546Spatrick        try:
241061da546Spatrick            summary = provider.num_children()
242061da546Spatrick        except:
243061da546Spatrick            summary = None
244061da546Spatrick        logger >> "got summary " + str(summary)
245061da546Spatrick        if summary is None:
246061da546Spatrick            summary = '<variable is not CFDictionary>'
247*f6aab3d8Srobert        if isinstance(summary, str):
248061da546Spatrick            return summary
249061da546Spatrick        else:
250061da546Spatrick            # needed on OSX Mountain Lion
251061da546Spatrick            if provider.sys_params.is_64_bit:
252061da546Spatrick                summary = summary & ~0x0f1f000000000000
253061da546Spatrick            summary = '@"' + str(summary) + \
254061da546Spatrick                (' entries"' if summary != 1 else ' entry"')
255061da546Spatrick        return summary
256061da546Spatrick    return 'Summary Unavailable'
257061da546Spatrick
258061da546Spatrick
259061da546Spatrickdef __lldb_init_module(debugger, dict):
260061da546Spatrick    debugger.HandleCommand(
261061da546Spatrick        "type summary add -F CFDictionary.CFDictionary_SummaryProvider NSDictionary")
262061da546Spatrick    debugger.HandleCommand(
263061da546Spatrick        "type summary add -F CFDictionary.CFDictionary_SummaryProvider2 CFDictionaryRef CFMutableDictionaryRef")
264