| 1 |
""" |
| 2 |
LLDB AppKit formatters |
| 3 |
|
| 4 |
Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 |
See https://llvm.org/LICENSE.txt for license information. |
| 6 |
SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 |
""" |
| 8 |
# example summary provider for NSArray |
| 9 |
# the real summary is now C++ code built into LLDB |
| 10 |
import lldb |
| 11 |
import ctypes |
| 12 |
import lldb.runtime.objc.objc_runtime |
| 13 |
import lldb.formatters.metrics |
| 14 |
import lldb.formatters.Logger |
| 15 |
|
| 16 |
statistics = lldb.formatters.metrics.Metrics() |
| 17 |
statistics.add_metric('invalid_isa') |
| 18 |
statistics.add_metric('invalid_pointer') |
| 19 |
statistics.add_metric('unknown_class') |
| 20 |
statistics.add_metric('code_notrun') |
| 21 |
|
| 22 |
# much less functional than the other two cases below |
| 23 |
# just runs code to get to the count and then returns |
| 24 |
# no children |
| 25 |
|
| 26 |
|
| 27 |
class NSArrayKVC_SynthProvider: |
| 28 |
|
| 29 |
def adjust_for_architecture(self): |
| 30 |
pass |
| 31 |
|
| 32 |
def __init__(self, valobj, dict, params): |
| 33 |
logger = lldb.formatters.Logger.Logger() |
| 34 |
self.valobj = valobj |
| 35 |
self.update() |
| 36 |
|
| 37 |
def update(self): |
| 38 |
logger = lldb.formatters.Logger.Logger() |
| 39 |
self.adjust_for_architecture() |
| 40 |
|
| 41 |
def num_children(self): |
| 42 |
logger = lldb.formatters.Logger.Logger() |
| 43 |
stream = lldb.SBStream() |
| 44 |
self.valobj.GetExpressionPath(stream) |
| 45 |
num_children_vo = self.valobj.CreateValueFromExpression( |
| 46 |
"count", "(int)[" + stream.GetData() + " count]") |
| 47 |
if num_children_vo.IsValid(): |
| 48 |
return num_children_vo.GetValueAsUnsigned(0) |
| 49 |
return "<variable is not NSArray>" |
| 50 |
|
| 51 |
# much less functional than the other two cases below |
| 52 |
# just runs code to get to the count and then returns |
| 53 |
# no children |
| 54 |
|
| 55 |
|
| 56 |
class NSArrayCF_SynthProvider: |
| 57 |
|
| 58 |
def adjust_for_architecture(self): |
| 59 |
pass |
| 60 |
|
| 61 |
def __init__(self, valobj, dict, params): |
| 62 |
logger = lldb.formatters.Logger.Logger() |
| 63 |
self.valobj = valobj |
| 64 |
self.sys_params = params |
| 65 |
if not (self.sys_params.types_cache.ulong): |
| 66 |
self.sys_params.types_cache.ulong = self.valobj.GetType( |
| 67 |
).GetBasicType(lldb.eBasicTypeUnsignedLong) |
| 68 |
self.update() |
| 69 |
|
| 70 |
def update(self): |
| 71 |
logger = lldb.formatters.Logger.Logger() |
| 72 |
self.adjust_for_architecture() |
| 73 |
|
| 74 |
def num_children(self): |
| 75 |
logger = lldb.formatters.Logger.Logger() |
| 76 |
num_children_vo = self.valobj.CreateChildAtOffset( |
| 77 |
"count", self.sys_params.cfruntime_size, self.sys_params.types_cache.ulong) |
| 78 |
return num_children_vo.GetValueAsUnsigned(0) |
| 79 |
|
| 80 |
|
| 81 |
class NSArrayI_SynthProvider: |
| 82 |
|
| 83 |
def adjust_for_architecture(self): |
| 84 |
pass |
| 85 |
|
| 86 |
def __init__(self, valobj, dict, params): |
| 87 |
logger = lldb.formatters.Logger.Logger() |
| 88 |
self.valobj = valobj |
| 89 |
self.sys_params = params |
| 90 |
if not(self.sys_params.types_cache.long): |
| 91 |
self.sys_params.types_cache.long = self.valobj.GetType( |
| 92 |
).GetBasicType(lldb.eBasicTypeLong) |
| 93 |
self.update() |
| 94 |
|
| 95 |
def update(self): |
| 96 |
logger = lldb.formatters.Logger.Logger() |
| 97 |
self.adjust_for_architecture() |
| 98 |
|
| 99 |
# skip the isa pointer and get at the size |
| 100 |
def num_children(self): |
| 101 |
logger = lldb.formatters.Logger.Logger() |
| 102 |
count = self.valobj.CreateChildAtOffset( |
| 103 |
"count", |
| 104 |
self.sys_params.pointer_size, |
| 105 |
self.sys_params.types_cache.long) |
| 106 |
return count.GetValueAsUnsigned(0) |
| 107 |
|
| 108 |
|
| 109 |
class NSArrayM_SynthProvider: |
| 110 |
|
| 111 |
def adjust_for_architecture(self): |
| 112 |
pass |
| 113 |
|
| 114 |
def __init__(self, valobj, dict, params): |
| 115 |
logger = lldb.formatters.Logger.Logger() |
| 116 |
self.valobj = valobj |
| 117 |
self.sys_params = params |
| 118 |
if not(self.sys_params.types_cache.long): |
| 119 |
self.sys_params.types_cache.long = self.valobj.GetType( |
| 120 |
).GetBasicType(lldb.eBasicTypeLong) |
| 121 |
self.update() |
| 122 |
|
| 123 |
def update(self): |
| 124 |
logger = lldb.formatters.Logger.Logger() |
| 125 |
self.adjust_for_architecture() |
| 126 |
|
| 127 |
# skip the isa pointer and get at the size |
| 128 |
def num_children(self): |
| 129 |
logger = lldb.formatters.Logger.Logger() |
| 130 |
count = self.valobj.CreateChildAtOffset( |
| 131 |
"count", |
| 132 |
self.sys_params.pointer_size, |
| 133 |
self.sys_params.types_cache.long) |
| 134 |
return count.GetValueAsUnsigned(0) |
| 135 |
|
| 136 |
# this is the actual synth provider, but is just a wrapper that checks |
| 137 |
# whether valobj is an instance of __NSArrayI or __NSArrayM and sets up an |
| 138 |
# appropriate backend layer to do the computations |
| 139 |
|
| 140 |
|
| 141 |
class NSArray_SynthProvider: |
| 142 |
|
| 143 |
def adjust_for_architecture(self): |
| 144 |
pass |
| 145 |
|
| 146 |
def __init__(self, valobj, dict): |
| 147 |
logger = lldb.formatters.Logger.Logger() |
| 148 |
self.valobj = valobj |
| 149 |
self.adjust_for_architecture() |
| 150 |
self.error = False |
| 151 |
self.wrapper = self.make_wrapper() |
| 152 |
self.invalid = (self.wrapper is None) |
| 153 |
|
| 154 |
def num_children(self): |
| 155 |
logger = lldb.formatters.Logger.Logger() |
| 156 |
if self.wrapper is None: |
| 157 |
return 0 |
| 158 |
return self.wrapper.num_children() |
| 159 |
|
| 160 |
def update(self): |
| 161 |
logger = lldb.formatters.Logger.Logger() |
| 162 |
if self.wrapper is None: |
| 163 |
return |
| 164 |
self.wrapper.update() |
| 165 |
|
| 166 |
# this code acts as our defense against NULL and uninitialized |
| 167 |
# NSArray pointers, which makes it much longer than it would be otherwise |
| 168 |
def make_wrapper(self): |
| 169 |
logger = lldb.formatters.Logger.Logger() |
| 170 |
if self.valobj.GetValueAsUnsigned() == 0: |
| 171 |
self.error = True |
| 172 |
return lldb.runtime.objc.objc_runtime.InvalidPointer_Description( |
| 173 |
True) |
| 174 |
else: |
| 175 |
global statistics |
| 176 |
class_data, wrapper = lldb.runtime.objc.objc_runtime.Utilities.prepare_class_detection( |
| 177 |
self.valobj, statistics) |
| 178 |
if wrapper: |
| 179 |
self.error = True |
| 180 |
return wrapper |
| 181 |
|
| 182 |
name_string = class_data.class_name() |
| 183 |
|
| 184 |
logger >> "Class name is " + str(name_string) |
| 185 |
|
| 186 |
if name_string == '__NSArrayI': |
| 187 |
wrapper = NSArrayI_SynthProvider( |
| 188 |
self.valobj, dict, class_data.sys_params) |
| 189 |
statistics.metric_hit('code_notrun', self.valobj.GetName()) |
| 190 |
elif name_string == '__NSArrayM': |
| 191 |
wrapper = NSArrayM_SynthProvider( |
| 192 |
self.valobj, dict, class_data.sys_params) |
| 193 |
statistics.metric_hit('code_notrun', self.valobj.GetName()) |
| 194 |
elif name_string == '__NSCFArray': |
| 195 |
wrapper = NSArrayCF_SynthProvider( |
| 196 |
self.valobj, dict, class_data.sys_params) |
| 197 |
statistics.metric_hit('code_notrun', self.valobj.GetName()) |
| 198 |
else: |
| 199 |
wrapper = NSArrayKVC_SynthProvider( |
| 200 |
self.valobj, dict, class_data.sys_params) |
| 201 |
statistics.metric_hit( |
| 202 |
'unknown_class', str( |
| 203 |
self.valobj.GetName()) + " seen as " + name_string) |
| 204 |
return wrapper |
| 205 |
|
| 206 |
|
| 207 |
def CFArray_SummaryProvider(valobj, dict): |
| 208 |
logger = lldb.formatters.Logger.Logger() |
| 209 |
provider = NSArray_SynthProvider(valobj, dict) |
| 210 |
if not provider.invalid: |
| 211 |
if provider.error: |
| 212 |
return provider.wrapper.message() |
| 213 |
try: |
| 214 |
summary = int(provider.num_children()) |
| 215 |
except: |
| 216 |
summary = None |
| 217 |
logger >> "provider gave me " + str(summary) |
| 218 |
if summary is None: |
| 219 |
summary = '<variable is not NSArray>' |
| 220 |
elif isinstance(summary, str): |
| 221 |
pass |
| 222 |
else: |
| 223 |
# we format it like it were a CFString to make it look the same as |
| 224 |
# the summary from Xcode |
| 225 |
summary = '@"' + str(summary) + \ |
| 226 |
(" objects" if summary != 1 else " object") + '"' |
| 227 |
return summary |
| 228 |
return 'Summary Unavailable' |
| 229 |
|
| 230 |
|
| 231 |
def __lldb_init_module(debugger, dict): |
| 232 |
debugger.HandleCommand( |
| 233 |
"type summary add -F CFArray.CFArray_SummaryProvider NSArray CFArrayRef CFMutableArrayRef") |