2012-03-14 05:52:00 +08:00
|
|
|
"""
|
|
|
|
LLDB AppKit formatters
|
|
|
|
|
2019-01-19 16:50:56 +08:00
|
|
|
Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-03-14 05:52:00 +08:00
|
|
|
"""
|
2012-09-05 02:47:54 +08:00
|
|
|
# example summary provider for NSArray
|
|
|
|
# the real summary is now C++ code built into LLDB
|
2012-02-24 07:10:27 +08:00
|
|
|
import lldb
|
|
|
|
import ctypes
|
2012-04-25 09:39:27 +08:00
|
|
|
import lldb.runtime.objc.objc_runtime
|
|
|
|
import lldb.formatters.metrics
|
|
|
|
import lldb.formatters.Logger
|
2012-02-24 07:10:27 +08:00
|
|
|
|
2019-03-25 23:21:29 +08:00
|
|
|
try:
|
|
|
|
basestring
|
|
|
|
except NameError:
|
|
|
|
basestring = str
|
|
|
|
|
2012-04-25 09:39:27 +08:00
|
|
|
statistics = lldb.formatters.metrics.Metrics()
|
2012-02-24 07:10:27 +08:00
|
|
|
statistics.add_metric('invalid_isa')
|
|
|
|
statistics.add_metric('invalid_pointer')
|
|
|
|
statistics.add_metric('unknown_class')
|
|
|
|
statistics.add_metric('code_notrun')
|
|
|
|
|
|
|
|
# much less functional than the other two cases below
|
|
|
|
# just runs code to get to the count and then returns
|
|
|
|
# no children
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
|
2012-02-24 07:10:27 +08:00
|
|
|
class NSArrayKVC_SynthProvider:
|
|
|
|
|
|
|
|
def adjust_for_architecture(self):
|
2012-03-06 03:56:33 +08:00
|
|
|
pass
|
2012-02-24 07:10:27 +08:00
|
|
|
|
2012-03-06 03:56:33 +08:00
|
|
|
def __init__(self, valobj, dict, params):
|
2012-04-25 09:39:27 +08:00
|
|
|
logger = lldb.formatters.Logger.Logger()
|
2012-02-24 07:10:27 +08:00
|
|
|
self.valobj = valobj
|
|
|
|
self.update()
|
|
|
|
|
|
|
|
def update(self):
|
2012-04-25 09:39:27 +08:00
|
|
|
logger = lldb.formatters.Logger.Logger()
|
2012-02-24 07:10:27 +08:00
|
|
|
self.adjust_for_architecture()
|
|
|
|
|
|
|
|
def num_children(self):
|
2012-04-25 09:39:27 +08:00
|
|
|
logger = lldb.formatters.Logger.Logger()
|
2012-02-24 07:10:27 +08:00
|
|
|
stream = lldb.SBStream()
|
|
|
|
self.valobj.GetExpressionPath(stream)
|
|
|
|
num_children_vo = self.valobj.CreateValueFromExpression(
|
|
|
|
"count", "(int)[" + stream.GetData() + " count]")
|
2012-03-14 05:52:00 +08:00
|
|
|
if num_children_vo.IsValid():
|
|
|
|
return num_children_vo.GetValueAsUnsigned(0)
|
|
|
|
return "<variable is not NSArray>"
|
2012-02-24 07:10:27 +08:00
|
|
|
|
|
|
|
# much less functional than the other two cases below
|
|
|
|
# just runs code to get to the count and then returns
|
|
|
|
# no children
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
|
2012-02-24 07:10:27 +08:00
|
|
|
class NSArrayCF_SynthProvider:
|
|
|
|
|
|
|
|
def adjust_for_architecture(self):
|
2012-03-06 03:56:33 +08:00
|
|
|
pass
|
2012-02-24 07:10:27 +08:00
|
|
|
|
2012-03-06 03:56:33 +08:00
|
|
|
def __init__(self, valobj, dict, params):
|
2012-04-25 09:39:27 +08:00
|
|
|
logger = lldb.formatters.Logger.Logger()
|
2012-02-24 07:10:27 +08:00
|
|
|
self.valobj = valobj
|
2012-03-06 03:56:33 +08:00
|
|
|
self.sys_params = params
|
|
|
|
if not (self.sys_params.types_cache.ulong):
|
|
|
|
self.sys_params.types_cache.ulong = self.valobj.GetType(
|
|
|
|
).GetBasicType(lldb.eBasicTypeUnsignedLong)
|
2012-02-24 07:10:27 +08:00
|
|
|
self.update()
|
|
|
|
|
|
|
|
def update(self):
|
2012-04-25 09:39:27 +08:00
|
|
|
logger = lldb.formatters.Logger.Logger()
|
2012-02-24 07:10:27 +08:00
|
|
|
self.adjust_for_architecture()
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2012-02-24 07:10:27 +08:00
|
|
|
def num_children(self):
|
2012-04-25 09:39:27 +08:00
|
|
|
logger = lldb.formatters.Logger.Logger()
|
2012-02-24 07:10:27 +08:00
|
|
|
num_children_vo = self.valobj.CreateChildAtOffset(
|
|
|
|
"count", self.sys_params.cfruntime_size, self.sys_params.types_cache.ulong)
|
|
|
|
return num_children_vo.GetValueAsUnsigned(0)
|
|
|
|
|
|
|
|
|
|
|
|
class NSArrayI_SynthProvider:
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2012-02-24 07:10:27 +08:00
|
|
|
def adjust_for_architecture(self):
|
2012-03-06 03:56:33 +08:00
|
|
|
pass
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2012-03-06 03:56:33 +08:00
|
|
|
def __init__(self, valobj, dict, params):
|
2012-04-25 09:39:27 +08:00
|
|
|
logger = lldb.formatters.Logger.Logger()
|
2012-02-24 07:10:27 +08:00
|
|
|
self.valobj = valobj
|
2012-03-06 03:56:33 +08:00
|
|
|
self.sys_params = params
|
|
|
|
if not(self.sys_params.types_cache.long):
|
|
|
|
self.sys_params.types_cache.long = self.valobj.GetType(
|
|
|
|
).GetBasicType(lldb.eBasicTypeLong)
|
2012-02-24 07:10:27 +08:00
|
|
|
self.update()
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2012-02-24 07:10:27 +08:00
|
|
|
def update(self):
|
2012-04-25 09:39:27 +08:00
|
|
|
logger = lldb.formatters.Logger.Logger()
|
2012-02-24 07:10:27 +08:00
|
|
|
self.adjust_for_architecture()
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2012-02-24 07:10:27 +08:00
|
|
|
# skip the isa pointer and get at the size
|
|
|
|
def num_children(self):
|
2012-04-25 09:39:27 +08:00
|
|
|
logger = lldb.formatters.Logger.Logger()
|
2012-02-24 07:10:27 +08:00
|
|
|
count = self.valobj.CreateChildAtOffset(
|
|
|
|
"count",
|
2012-03-06 03:56:33 +08:00
|
|
|
self.sys_params.pointer_size,
|
|
|
|
self.sys_params.types_cache.long)
|
|
|
|
return count.GetValueAsUnsigned(0)
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2012-02-24 07:10:27 +08:00
|
|
|
|
|
|
|
class NSArrayM_SynthProvider:
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2012-02-24 07:10:27 +08:00
|
|
|
def adjust_for_architecture(self):
|
2012-03-06 03:56:33 +08:00
|
|
|
pass
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2012-03-06 03:56:33 +08:00
|
|
|
def __init__(self, valobj, dict, params):
|
2012-04-25 09:39:27 +08:00
|
|
|
logger = lldb.formatters.Logger.Logger()
|
2012-02-24 07:10:27 +08:00
|
|
|
self.valobj = valobj
|
2012-03-06 03:56:33 +08:00
|
|
|
self.sys_params = params
|
|
|
|
if not(self.sys_params.types_cache.long):
|
|
|
|
self.sys_params.types_cache.long = self.valobj.GetType(
|
|
|
|
).GetBasicType(lldb.eBasicTypeLong)
|
|
|
|
self.update()
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2012-02-24 07:10:27 +08:00
|
|
|
def update(self):
|
2012-04-25 09:39:27 +08:00
|
|
|
logger = lldb.formatters.Logger.Logger()
|
2012-02-24 07:10:27 +08:00
|
|
|
self.adjust_for_architecture()
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2012-02-24 07:10:27 +08:00
|
|
|
# skip the isa pointer and get at the size
|
|
|
|
def num_children(self):
|
2012-04-25 09:39:27 +08:00
|
|
|
logger = lldb.formatters.Logger.Logger()
|
2012-02-24 07:10:27 +08:00
|
|
|
count = self.valobj.CreateChildAtOffset(
|
|
|
|
"count",
|
2012-03-06 03:56:33 +08:00
|
|
|
self.sys_params.pointer_size,
|
|
|
|
self.sys_params.types_cache.long)
|
|
|
|
return count.GetValueAsUnsigned(0)
|
2012-02-24 07:10:27 +08:00
|
|
|
|
|
|
|
# this is the actual synth provider, but is just a wrapper that checks
|
|
|
|
# whether valobj is an instance of __NSArrayI or __NSArrayM and sets up an
|
|
|
|
# appropriate backend layer to do the computations
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
|
2012-02-24 07:10:27 +08:00
|
|
|
class NSArray_SynthProvider:
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2012-02-24 07:10:27 +08:00
|
|
|
def adjust_for_architecture(self):
|
2012-03-06 03:56:33 +08:00
|
|
|
pass
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2012-02-24 07:10:27 +08:00
|
|
|
def __init__(self, valobj, dict):
|
2012-04-25 09:39:27 +08:00
|
|
|
logger = lldb.formatters.Logger.Logger()
|
2012-02-24 07:10:27 +08:00
|
|
|
self.valobj = valobj
|
|
|
|
self.adjust_for_architecture()
|
2012-03-14 05:52:00 +08:00
|
|
|
self.error = False
|
|
|
|
self.wrapper = self.make_wrapper()
|
2012-02-24 07:10:27 +08:00
|
|
|
self.invalid = (self.wrapper is None)
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2012-02-24 07:10:27 +08:00
|
|
|
def num_children(self):
|
2012-04-25 09:39:27 +08:00
|
|
|
logger = lldb.formatters.Logger.Logger()
|
2012-02-24 07:10:27 +08:00
|
|
|
if self.wrapper is None:
|
|
|
|
return 0
|
|
|
|
return self.wrapper.num_children()
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2012-02-24 07:10:27 +08:00
|
|
|
def update(self):
|
2012-04-25 09:39:27 +08:00
|
|
|
logger = lldb.formatters.Logger.Logger()
|
2012-02-24 07:10:27 +08:00
|
|
|
if self.wrapper is None:
|
2012-03-06 03:56:33 +08:00
|
|
|
return
|
|
|
|
self.wrapper.update()
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-09-22 13:07:56 +08:00
|
|
|
# this code acts as our defense against NULL and uninitialized
|
2012-02-24 07:10:27 +08:00
|
|
|
# NSArray pointers, which makes it much longer than it would be otherwise
|
2012-03-14 05:52:00 +08:00
|
|
|
def make_wrapper(self):
|
2012-04-25 09:39:27 +08:00
|
|
|
logger = lldb.formatters.Logger.Logger()
|
2012-03-14 05:52:00 +08:00
|
|
|
if self.valobj.GetValueAsUnsigned() == 0:
|
|
|
|
self.error = True
|
2012-04-26 04:59:02 +08:00
|
|
|
return lldb.runtime.objc.objc_runtime.InvalidPointer_Description(
|
|
|
|
True)
|
2012-03-14 05:52:00 +08:00
|
|
|
else:
|
|
|
|
global statistics
|
2012-04-26 01:53:41 +08:00
|
|
|
class_data, wrapper = lldb.runtime.objc.objc_runtime.Utilities.prepare_class_detection(
|
|
|
|
self.valobj, statistics)
|
2012-03-14 05:52:00 +08:00
|
|
|
if wrapper:
|
|
|
|
self.error = True
|
|
|
|
return wrapper
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2012-02-24 07:10:27 +08:00
|
|
|
name_string = class_data.class_name()
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2012-04-03 00:39:29 +08:00
|
|
|
logger >> "Class name is " + str(name_string)
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2012-02-24 07:10:27 +08:00
|
|
|
if name_string == '__NSArrayI':
|
2012-03-14 05:52:00 +08:00
|
|
|
wrapper = NSArrayI_SynthProvider(
|
|
|
|
self.valobj, dict, class_data.sys_params)
|
2012-04-03 07:43:22 +08:00
|
|
|
statistics.metric_hit('code_notrun', self.valobj.GetName())
|
2012-02-24 07:10:27 +08:00
|
|
|
elif name_string == '__NSArrayM':
|
2012-03-14 05:52:00 +08:00
|
|
|
wrapper = NSArrayM_SynthProvider(
|
|
|
|
self.valobj, dict, class_data.sys_params)
|
2012-04-03 07:43:22 +08:00
|
|
|
statistics.metric_hit('code_notrun', self.valobj.GetName())
|
2012-02-24 07:10:27 +08:00
|
|
|
elif name_string == '__NSCFArray':
|
2012-03-14 05:52:00 +08:00
|
|
|
wrapper = NSArrayCF_SynthProvider(
|
|
|
|
self.valobj, dict, class_data.sys_params)
|
2012-04-03 07:43:22 +08:00
|
|
|
statistics.metric_hit('code_notrun', self.valobj.GetName())
|
2012-02-24 07:10:27 +08:00
|
|
|
else:
|
2012-03-14 05:52:00 +08:00
|
|
|
wrapper = NSArrayKVC_SynthProvider(
|
|
|
|
self.valobj, dict, class_data.sys_params)
|
2012-04-03 07:43:22 +08:00
|
|
|
statistics.metric_hit(
|
|
|
|
'unknown_class', str(
|
|
|
|
self.valobj.GetName()) + " seen as " + name_string)
|
2012-02-24 07:10:27 +08:00
|
|
|
return wrapper
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
|
2012-02-24 07:10:27 +08:00
|
|
|
def CFArray_SummaryProvider(valobj, dict):
|
2012-04-25 09:39:27 +08:00
|
|
|
logger = lldb.formatters.Logger.Logger()
|
2012-02-24 07:10:27 +08:00
|
|
|
provider = NSArray_SynthProvider(valobj, dict)
|
|
|
|
if not provider.invalid:
|
2012-03-14 05:52:00 +08:00
|
|
|
if provider.error:
|
|
|
|
return provider.wrapper.message()
|
|
|
|
try:
|
|
|
|
summary = int(provider.num_children())
|
|
|
|
except:
|
|
|
|
summary = None
|
2012-04-03 00:39:29 +08:00
|
|
|
logger >> "provider gave me " + str(summary)
|
2012-03-14 05:52:00 +08:00
|
|
|
if summary is None:
|
|
|
|
summary = '<variable is not NSArray>'
|
|
|
|
elif isinstance(summary, basestring):
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
# we format it like it were a CFString to make it look the same as
|
|
|
|
# the summary from Xcode
|
|
|
|
summary = '@"' + str(summary) + \
|
|
|
|
(" objects" if summary != 1 else " object") + '"'
|
|
|
|
return summary
|
|
|
|
return 'Summary Unavailable'
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
|
2012-02-24 07:10:27 +08:00
|
|
|
def __lldb_init_module(debugger, dict):
|
|
|
|
debugger.HandleCommand(
|
|
|
|
"type summary add -F CFArray.CFArray_SummaryProvider NSArray CFArrayRef CFMutableArrayRef")
|